Skip to content

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

@jonasvdd

Description

@jonasvdd
  • pandas version: 2.0.1
  • plotly_resampler version: 0.9.0rc1

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:

axis_type = "date" if isinstance(dc.x, pd.DatetimeIndex) else "linear"

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:

  • verify if this occurs and works for pandas <2
    Indeed this, occurs aswell for pandas<2
  • create a PR and add tests for this.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions