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

Serialization of Pandas' Timestamp #442

Closed
odelmarcelle opened this issue Nov 23, 2023 · 0 comments
Closed

Serialization of Pandas' Timestamp #442

odelmarcelle opened this issue Nov 23, 2023 · 0 comments
Labels

Comments

@odelmarcelle
Copy link

I was wondering why the serialization of Pandas' Timestamp object was not possible even though they inherit from datetime.datetime.

>>> import orjson
>>> orjson.__version__
'3.9.10'
>>> import pandas as pd
>>> import datetime
dt = datetime.datetime(2023, 11, 23)
>>> orjson.dumps(dt)
b'"2023-11-23T00:00:00"'
>>> ts = pd.Timestamp(dt)
>>> isinstance(ts, datetime.datetime)
True
>>> orjson.dumps(ts)
TypeError: Type is not JSON serializable: Timestamp

Wouldn't it better to check first if the object to serialize is a subclass of serializable types?

It already seems to be the case for classes inheriting from dict, but not from datetime.datetime.

>>> class CustomDT(datetime.datetime):
>>>     pass
>>> cdt = CustomDT(2023, 11, 23)
>>> orjson.dumps(cdt)
TypeError: Type is not JSON serializable: CustomDT
>>> class CustomDict(dict):
>>>     pass
>>> cdict = CustomDict([('key', 'value')])
>>> orjson.dumps(cdict)
b'{"key":"value"}'
@github-actions github-actions bot added the Stale label Dec 1, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Dec 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant