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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!
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