predict-idlab / plotly-resampler

Visualize large time series data with plotly.py
https://predict-idlab.github.io/plotly-resampler/latest
MIT License
1k stars 67 forks source link

Non-index date time series as hf_x and pandas v2.0 compatibility #208

Closed jonasvdd closed 1 year ago

jonasvdd commented 1 year ago

code snippet which fails

import pandas as pd; import numpy as np
from plotly_resampler import FigureResampler

df = pd.DataFrame({'timestamp': pd.date_range('2020-01-01', '2020-01-02', freq='1s')})
df['value'] = np.random.randn(len(df))

fr = FigureResampler()
# This works
# fr.add_trace({}, hf_x=pd.Index(df.timestamp), hf_y=df.value)
# This doesn't
fr.add_trace({}, hf_x=df.timestamp, hf_y=df.value)
fr

What causes the fail:

this line: https://github.com/predict-idlab/plotly-resampler/blob/e56cbc71d32991c68e5763d3652394aff83cf94a/plotly_resampler/figure_resampler/figure_resampler_interface.py#L787

Will give that the datetime64 array requires a linear axis. As such, the xaxis.range[0] and xaxis.range[1] arguments will not be parsed to timestamps, and the hf_x slicing (i.e. the bisect), will fail, resulting in this error: image

Solution

extend the check with:

axis_type = (
    "date"
    if isinstance(dc.x, pd.DatetimeIndex) or
    pd.core.dtypes.common.is_datetime64_any_dtype(dc.x)
    else "linear"
)

TODO:

jonasvdd commented 1 year ago

@jvdd will you create a new release candidate?

jvdd commented 1 year ago

Done! plotly-resampler 0.9.0rc2