Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow serialization overwrite, or different serialization on specific component/props #4058

Open
LeoGrosjean opened this issue Oct 4, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@LeoGrosjean
Copy link
Contributor

Describe the bug
Can't overwrite an existing serializer for a defined type (here pd.DataFrame

To Reproduce

@rx.serializer
def serialize_dataframe(dataframe: pd.DataFrame) -> List[Dict[str, Any]]:
        """
        Serialize a pandas DataFrame to dict with records
        """
        return dataframe.to_dict("records")

usage
In my vega/altair wrapper I need to serialize dataframe to records (list of dict[col, value]

quickfix fail

def serializer(
    fn: Serializer | None = None,
    to: Type | None = None,
    overwrite: bool = False, # <- yuhu
) -> Serializer:
        ...
        if registered_fn is not None and (registered_fn != fn and not overwrite):
        raise ValueError(
            f"Serializer for type {type_} is already registered as {registered_fn.__qualname__}."
        )

But when app is starting, serializer is called with overwrite default value ! So False instaid of True !

@LeoGrosjean LeoGrosjean added the bug Something isn't working label Oct 4, 2024
@adhami3310
Copy link
Member

Why is not possible to use the value of the current serializer of dataframe?

@LeoGrosjean
Copy link
Contributor Author

I wrapped altair/vega component here

But vega-lite/vega needs data like this :

df.to_dict("records")
[
  {'date': Timestamp('2012-01-01 00:00:00'),
  'precipitation': 0.0,
  'temp_max': 12.8,
  'temp_min': 5.0,
  'weather': 'drizzle',
  'wind': 4.7},
  {'date': Timestamp('2012-01-02 00:00:00'),
  'precipitation': 10.9,
  'temp_max': 10.6,
  'temp_min': 2.8,
  'weather': 'rain',
  'wind': 4.5}
]

instead of

df.to_dict()
{'date': {0: Timestamp('2012-01-01 00:00:00'),
        1: Timestamp('2012-01-02 00:00:00')},
'precipitation': {0: 0.0, 1: 10.9},
'temp_max': {0: 12.8, 1: 10.6},
'temp_min': {0: 5.0, 1: 2.8},
'weather': {0: 'drizzle', 1: 'rain'},
'wind': {0: 4.7, 1: 4.5}}

Its not a major issue, just for convenience

@LeoGrosjean
Copy link
Contributor Author

What do you think of something like :

DataFrameAltair= Annotated[pd.DataFrame, {"orient": "records "}]

And then in the serializer check for annotation ?

@LeoGrosjean
Copy link
Contributor Author

LeoGrosjean commented Oct 8, 2024

I succeed !

Will tidy my code next week and add tests

Serializer is created, but not used :(

@adhami3310
Copy link
Member

dang it, that seemed like a good solution

is it possible to wrap dataframe some other way? like with an actual type and not an alias

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants