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

Creating a timeline does not work with [new] pandas 2.0.0 #4102

Closed
kaibir opened this issue Mar 14, 2023 · 1 comment · Fixed by #4103
Closed

Creating a timeline does not work with [new] pandas 2.0.0 #4102

kaibir opened this issue Mar 14, 2023 · 1 comment · Fixed by #4103

Comments

@kaibir
Copy link
Contributor

kaibir commented Mar 14, 2023

Description:

When test-driving the new pandas release candidate 2.0.0rc0 we noticed that generating Plotly graphs failed (snippet for reference below).

When investigating, we found the following in the Plotly code: (x_end - x_start).astype("timedelta64[ms]"). With Pandas < 2 this returned float64 representing the ms between the timestamps.

Now that Pandas 2 has a timedelta64[ms] dtype this dtype is used instead.

import pandas as pd

x_start = pd.Series(pd.Timestamp(2023,3,14,12))
x_end = pd.Series(pd.Timestamp(2023,3,14,14))

# Pandas 1.5.3
# 0    7200000.0
# dtype: float64
# 
# Pandas 2.0.0rc0
# 0   0 days 02:00:00
# dtype: timedelta64[ms]
(x_end - x_start).astype("timedelta64[ms]")

But Plotly expects a float64 dtype and we run into an issue, that timedelta is not JSON serializable (see Plotly reproduction below)

The following works with all Pandas versions (especially 1.5.3 and 2.0.0rc0):

(x_end - x_start).astype("timedelta64[ns]") / pd.Timedelta(1, 'ms')`

Plotly reproduction (with pandas 2.0.0rc0

import plotly.express as px
import pandas as pd

df = pd.DataFrame([{"Task": "Job A", "Start": "2022-01-01", "Finish": "2022-01-02"}])
fig_1 = px.timeline(df, x_start="Start", x_end="Finish", y="Task")
fig_1.update_yaxes(autorange="reversed")

leads to

[...]
File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/encoder.py:258, in JSONEncoder.iterencode(self, o, _one_shot)
    253 else:
    254     _iterencode = _make_iterencode(
    255         markers, self.default, _encoder, self.indent, floatstr,
    256         self.key_separator, self.item_separator, self.sort_keys,
    257         self.skipkeys, _one_shot)
--> 258 return _iterencode(o, 0)

File ~/venv/weplan3.11/lib/python3.11/site-packages/_plotly_utils/utils.py:136, in PlotlyJSONEncoder.default(self, obj)
    134     except NotEncodable:
    135         pass
--> 136 return _json.JSONEncoder.default(self, obj)

File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/encoder.py:180, in JSONEncoder.default(self, o)
    161 def default(self, o):
    162     """Implement this method in a subclass such that it returns
    163     a serializable object for ``o``, or calls the base implementation
    164     (to raise a ``TypeError``).
   (...)
    178 
    179     """
--> 180     raise TypeError(f'Object of type {o.__class__.__name__} '
    181                     f'is not JSON serializable')

TypeError: Object of type timedelta is not JSON serializable
@aberres
Copy link

aberres commented Mar 15, 2023

@kaibir kaibir changed the title Creating a timeline does not work with new pandas 2.0.0rc0 Creating a timeline does not work with [new] pandas 2.0.0 Apr 4, 2023
alexcjohnson added a commit that referenced this issue Apr 5, 2023
Pandas 2 compability: Fix a bug regarding timedelta64[ms] representation #4102
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants