From 4e7d41db543e1f2533a9f9edbe7c07aaad7eb810 Mon Sep 17 00:00:00 2001 From: Enricoftw <150397669+Enricoftw@users.noreply.github.com> Date: Sat, 21 Nov 2026 17:00:21 +0100 Subject: [PATCH] Fix px.timeline dropping fractional milliseconds from bar lengths Assisted-by: Claude Opus 5.5 --- CHANGELOG.md | 1 + plotly/express/_core.py | 11 ++++++++--- tests/test_optional/test_px/test_px_functions.py | 9 +++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8cc2568a0b..8b707797c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). - Fix `convert_dash` mapping dotted lines (`linestyle=':'`) to invalid `'circle'` in Plotly, correctly converting them to `'dot'` and preserving custom dash patterns as pixel lists, [[#5719](https://github.com/plotly/plotly.py/pull/5719)] with thanks to @robertoffmoura for the contribution! - Fix Plotly Express mutating lists passed to the `x` or `y` arguments in wide mode [[#5727](https://github.com/plotly/plotly.py/pull/5727)], with thanks to @cpruijsen for the contribution! - Fix `px.sunburst`, `px.treemap` and `px.icicle` listing sectors in a different order on every run when `path` is used with a Polars DataFrame; sectors now follow their order of first appearance for all dataframe backends [[#5766](https://github.com/plotly/plotly.py/pull/5766)], with thanks to @Irahan2 for the contribution! +- 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)] - Fix `trendline_scope="overall"` drawing the overall trendline on the `marginal_x`/`marginal_y` subplots as well as the main plot; it is now drawn only on the main subplots [[#5780](https://github.com/plotly/plotly.py/pull/5780)], with thanks to @jayzhou2309 for the contribution! - Fix `mpl_to_plotly` conversion of matplotlib contour lines and line collections: close contour rings ending with `Z` codes, support dash styles, convert date x-axes, separate disjoint subpaths with `None` separators, hide line collection traces from the legend by default, and group consecutive same-style lines into single traces [[#5770](https://github.com/plotly/plotly.py/pull/5770)], with thanks to @robertoffmoura for the contribution! - Fix `plotly.io.write_images` ignoring `layout.width`/`layout.height` (and the template's) and exporting every figure at the default 700x500 size; it now uses the same width/height priority as `write_image` and `to_image` [[#5784](https://github.com/plotly/plotly.py/pull/5784)], with thanks to @breken-ai for the contribution! diff --git a/plotly/express/_core.py b/plotly/express/_core.py index 3b447f7c02..08614ec65f 100644 --- a/plotly/express/_core.py +++ b/plotly/express/_core.py @@ -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"] diff --git a/tests/test_optional/test_px/test_px_functions.py b/tests/test_optional/test_px/test_px_functions.py index 84a451733b..8b9d4742d2 100644 --- a/tests/test_optional/test_px/test_px_functions.py +++ b/tests/test_optional/test_px/test_px_functions.py @@ -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", [