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

Adding datetime.date Support #2199

Closed
zacharylawrence opened this issue Jun 8, 2020 · 3 comments
Closed

Adding datetime.date Support #2199

zacharylawrence opened this issue Jun 8, 2020 · 3 comments

Comments

@zacharylawrence
Copy link

I looked around for an existing issue, but didn't see one. Please let me know if I should respond somewhere else instead!

Currently, putting a datetime.date in an altair chart gives the error:

TypeError: Object of type date is not JSON serializable

Are there ideas to support datetime.date in the future?

For example:

data = pd.DataFrame({
    "time": [datetime.date(2020, 5, 1), datetime.date(2020, 5, 3)],
    "value": [3, 5],
})

alt.Chart(data).mark_line().encode(
    x="time",
    y="value",
)
@jakevdp
Copy link
Collaborator

jakevdp commented Jun 8, 2020

The way to support that would be to update sanitize_dataframe to convert these into serializable ISO timestamps: https://github.com/altair-viz/altair/blob/03f37ca8b2701bf8d9e61646cacf8eee2edcad87/altair/utils/core.py#L243

It hasn't been done yet because it hasn't come up very often: generally timestamps in dataframes come in datetime-typed columns, rather than object-typed columns containing Python dates.

An easy workaround is to do that conversion yourself:

df.time = pd.to_datetime(df.time)

@zacharylawrence
Copy link
Author

Thanks for the clear explanation! Would you be open to a PR to this effect?

To add some background, this has come up from reading a parquet file with DATE type column: https://github.com/apache/parquet-format/blob/master/LogicalTypes.md#date

@jakevdp
Copy link
Collaborator

jakevdp commented Jun 9, 2020

Sure, a PR would be great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants