TimeUnit 'day' parsed differently for Pandas DataFrame and .csv data #3737
-
When creating a chart from a Pandas dataframe with a Example:
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
Thanks - this is an issue with how the dates are serialized to CSV combined with the fact that Javascript date parsing chooses a different timezone depending on how the date is formatted. In general, I recommend avoiding CSV serializations of data for many reasons including this one. |
Beta Was this translation helpful? Give feedback.
-
Is the solution to use JSON instead? Or not use datetime formats with datasets that are large enough to require storing in a file? |
Beta Was this translation helpful? Give feedback.
-
CSV is fine as long as dates are represented by the full ISO-8601 string. If you use Altair's data transformers rather than pandas, it will do the right thing. The fundamental issue stems from javascript's built-in date parsing, which is what the Vega-Lite renderer uses to parse dates stored as strings: > new Date('2020-11-20')
Thu Nov 19 2020 16:00:00 GMT-0800 (Pacific Standard Time)
> new Date('2020-11-20T00:00:00')
Fri Nov 20 2020 00:00:00 GMT-0800 (Pacific Standard Time) When you pass a dataframe to |
Beta Was this translation helpful? Give feedback.
-
I see. Thanks for the explanation. |
Beta Was this translation helpful? Give feedback.
CSV is fine as long as dates are represented by the full ISO-8601 string. If you use Altair's data transformers rather than pandas, it will do the right thing.
The fundamental issue stems from javascript's built-in date parsing, which is what the Vega-Lite renderer uses to parse dates stored as strings:
When you pass a dataframe to
alt.Chart
, Altair ensures that dates are serialized as full ISO-8601 strings, and so they will be parsed correctly. Pandasto_csv()
does not do this, and so Javascript parses the resulting…