plotly / plotly.py

The interactive graphing library for Python :sparkles: This project now includes Plotly Express!
https://plotly.com/python/
MIT License
15.62k stars 2.51k forks source link

`px.timeline` with a Daylight Savings Time scenario - missing/extra hour #4611

Open rhelouREV opened 1 month ago

rhelouREV commented 1 month ago

Problem: When using px.timeline on a range where a Daylight Savings Time switch occurs & time zones are in the timestamps, I observe an unexpectedly shorter/longer duration of the bar.

Example A - March - missing an hour

import pandas as pd
import plotly.express as px

df = pd.DataFrame(dict(
    x_start=[
        pd.Timestamp("2024-03-09", tz="US/Pacific"),
        pd.Timestamp("2024-03-10", tz="US/Pacific"),
        pd.Timestamp("2024-03-11", tz="US/Pacific"),
    ],
    x_end=[
        pd.Timestamp("2024-03-10", tz="US/Pacific"),
        pd.Timestamp("2024-03-11", tz="US/Pacific"),
        pd.Timestamp("2024-03-12", tz="US/Pacific"),
    ],
    y=[1, 1, 1],
))

px.timeline(df, x_start="x_start", x_end="x_end", y="y")

image

As you can see, for the DST day (March 10, 2024), the end date is unexpectedly 1 hour early, unlike the other days.

Example B - November - extra hour

import pandas as pd
import plotly.express as px

df = pd.DataFrame(dict(
    x_start=[
        pd.Timestamp("2023-11-05", tz="US/Pacific"),
    ],
    x_end=[
        pd.Timestamp("2023-11-06", tz="US/Pacific"),
    ],
    y=[1],
))

px.timeline(df, x_start="x_start", x_end="x_end", y="y")

image

In this example, 1 hour is unexpectedly added to the end.

Testing environment: