Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

### Fixed
- Fix `px.timeline` bar lengths dropping fractional milliseconds, which gave a 500 microsecond task a zero-width bar, and coming out 1 ms short for some durations with pandas, such as 1001 ms drawn as 1000 ms [[#5898](https://github.com/plotly/plotly.py/pull/5898)]

## [7.2.0] - 2026-11-24

Expand Down
11 changes: 8 additions & 3 deletions plotly/express/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2194,11 +2194,16 @@ def process_dataframe_timeline(args):
"Both x_start and x_end must refer to data convertible to datetimes."
) from exc

duration = nw.col(args["x_end"]) - nw.col(args["x_start"])
unit = df.head(0).select(duration).schema[args["x_end"]].time_unit
ticks = duration.cast(nw.Int64)
# Keep fractional milliseconds. Integer divisors let pyarrow divide large ticks.
per_ms = {"ms": 1, "us": 1_000, "ns": 1_000_000}
milliseconds = ticks * 1000 if unit == "s" else ticks / per_ms[unit]
# note that we are not adding any columns to the data frame here, so no risk of overwrite
args["data_frame"] = df.with_columns(
(nw.col(args["x_end"]) - nw.col(args["x_start"]))
.dt.total_milliseconds()
.alias(args["x_end"])
# pandas casts NaT to an integer, so keep missing durations missing
nw.when(~duration.is_null()).then(milliseconds).alias(args["x_end"])
)
args["x"] = args["x_end"]
args["base"] = args["x_start"]
Expand Down
9 changes: 9 additions & 0 deletions tests/test_optional/test_px/test_px_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,15 @@ def test_timeline(constructor):
px.timeline(df, x_start="Start", x_end=["a", "b", "c"], y="Task", color="Task")


def test_timeline_fractional_milliseconds(constructor):
start = np.array(["2026-01-01"] * 3 + ["NaT"], "datetime64[us]")
ticks = np.array([500, 1_001_000, -2_500, 0], "timedelta64[us]")
df = constructor({"Start": start, "Finish": start + ticks, "Task": list("ABCD")})
fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task")
assert fig.data[0].x.dtype == np.float64
assert_array_equal(fig.data[0].x, [0.5, 1001.0, -2.5, np.nan])


@pytest.mark.parametrize(
"datetime_columns",
[
Expand Down