refactor: Speed up function _serialize_series by 234% in PR #6044 (refactor-serialization) (#6079)

* feat: Implement serialization functions for various data types and add a unified serialize method

* optmize conditional

Co-authored-by: codeflash-ai[bot] <148906541+codeflash-ai[bot]@users.noreply.github.com>

* fix: Update string and list truncation to include ellipsis for clarity

* ️ Speed up function `_serialize_series` by 234% in PR #6044 (`refactor-serialization`)
Certainly! Here is a more optimized version of the program.



Changes made.
1. Replaced the `apply` method with dictionary comprehension. This avoids creating an intermediate Series, which can be an expensive operation.
2. Moved `_truncate_value` outside of the main function to keep the main function concise and focused.

* refactor: Remove unused `_truncate_value` function from serialization module

---------

Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@langflow.org>
Co-authored-by: codeflash-ai[bot] <148906541+codeflash-ai[bot]@users.noreply.github.com>
This commit is contained in:
codeflash-ai[bot] 2025-02-03 15:39:58 +00:00 committed by GitHub
commit 4fd2b2feb1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -128,8 +128,7 @@ def _serialize_series(obj: pd.Series, max_length: int | None, max_items: int | N
"""Serialize pandas Series to a dictionary format."""
if max_items is not None and len(obj) > max_items:
obj = obj.head(max_items)
obj = obj.apply(lambda x: _truncate_value(x, max_length, max_items))
return obj.to_dict()
return {index: _truncate_value(value, max_length, max_items) for index, value in obj.items()}
def _is_numpy_type(obj: Any) -> bool: