Skip to content

Commit 4e7d41d

Browse files
committed
Fix px.timeline dropping fractional milliseconds from bar lengths
Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2427c64 commit 4e7d41d

3 files changed

Lines changed: 18 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
2525
- 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!
2626
- 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!
2727
- 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!
28+
- 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)]
2829
- 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!
2930
- 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!
3031
- 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!

‎plotly/express/_core.py‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2194,11 +2194,16 @@ def process_dataframe_timeline(args):
21942194
"Both x_start and x_end must refer to data convertible to datetimes."
21952195
) from exc
21962196

2197+
duration = nw.col(args["x_end"]) - nw.col(args["x_start"])
2198+
unit = df.head(0).select(duration).schema[args["x_end"]].time_unit
2199+
ticks = duration.cast(nw.Int64)
2200+
# Keep fractional milliseconds. Integer divisors let pyarrow divide large ticks.
2201+
per_ms = {"ms": 1, "us": 1_000, "ns": 1_000_000}
2202+
milliseconds = ticks * 1000 if unit == "s" else ticks / per_ms[unit]
21972203
# note that we are not adding any columns to the data frame here, so no risk of overwrite
21982204
args["data_frame"] = df.with_columns(
2199-
(nw.col(args["x_end"]) - nw.col(args["x_start"]))
2200-
.dt.total_milliseconds()
2201-
.alias(args["x_end"])
2205+
# pandas casts NaT to an integer, so keep missing durations missing
2206+
nw.when(~duration.is_null()).then(milliseconds).alias(args["x_end"])
22022207
)
22032208
args["x"] = args["x_end"]
22042209
args["base"] = args["x_start"]

‎tests/test_optional/test_px/test_px_functions.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,15 @@ def test_timeline(constructor):
654654
px.timeline(df, x_start="Start", x_end=["a", "b", "c"], y="Task", color="Task")
655655

656656

657+
def test_timeline_fractional_milliseconds(constructor):
658+
start = np.array(["2026-01-01"] * 3 + ["NaT"], "datetime64[us]")
659+
ticks = np.array([500, 1_001_000, -2_500, 0], "timedelta64[us]")
660+
df = constructor({"Start": start, "Finish": start + ticks, "Task": list("ABCD")})
661+
fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task")
662+
assert fig.data[0].x.dtype == np.float64
663+
assert_array_equal(fig.data[0].x, [0.5, 1001.0, -2.5, np.nan])
664+
665+
657666
@pytest.mark.parametrize(
658667
"datetime_columns",
659668
[

0 commit comments

Comments
 (0)