8000 fix: px.timeline was failing when x_start and/or x_end were already d… · Coding-with-Adam/plotly.py@0916930 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0916930

Browse files
committed
fix: px.timeline was failing when x_start and/or x_end were already datetime
1 parent 8f50e2b commit 0916930

File tree

2 files changed

+42
-10
lines changed

2 files changed

+42
-10
lines changed

packages/python/plotly/plotly/express/_core.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2121,16 +2121,21 @@ def process_dataframe_timeline(args):
21212121
if args["x_start"] is None or args["x_end"] is None:
21222122
raise ValueError("Both x_start and x_end are required")
21232123

2124-
try:
2125-
df: nw.DataFrame = args["data_frame"]
2126-
df = df.with_columns(
2127-
nw.col(args["x_start"]).str.to_datetime().alias(args["x_start"]),
2128-
nw.col(args["x_end"]).str.to_datetime().alias(args["x_end"]),
2129-
)
2130-
except Exception:
2131-
raise TypeError(
2132-
"Both x_start and x_end must refer to data convertible to datetimes."
2133-
)
2124+
df: nw.DataFrame = args["data_frame"]
2125+
schema = df.schema
2126+
to_convert_to_datetime = [
2127+
col
2128+
for col in [args["x_start"], args["x_end"]]
2129+
if schema[col] != nw.Datetime and schema[col] != nw.Date
2130+
]
2131+
2132+
if to_convert_to_datetime:
2133+
try:
2134+
df = df.with_columns(nw.col(to_convert_to_datetime).str.to_datetime())
2135+
except Exception as exc:
2136+
raise TypeError(
2137+
"Both x_start and x_end must refer to data convertible to datetimes."
2138+
) from exc
21342139

21352140
# note that we are not adding any columns to the data frame here, so no risk of overwrite
21362141
args["data_frame"] = df.with_columns(

packages/python/plotly/plotly/tests/test_optional/test_px/test_px_functions.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,3 +561,30 @@ def test_timeline(constructor):
561561
msg = "Both x_start and x_end must refer to data convertible to datetimes."
562562
with pytest.raises(TypeError, match=msg):
563563
px.timeline(df, x_start="Start", x_end=["a", "b", "c"], y="Task", color="Task")
564+
565+
566+
@pytest.mark.parametrize(
567+
"datetime_columns",
568+
[
569+
["Start"],
570+
["Start", "Finish"],
571+
["Finish"],
572+
],
573+
)
574+
def test_timeline_cols_already_temporal(constructor, datetime_columns: list[str]):
575+
# https://github.com/plotly/plotly.py/issues/4913
576+
data = {
577+
"Task": ["Job A", "Job B", "Job C"],
578+
"Start": ["2009-01-01", "2009-03-05", "2009-02-20"],
579+
"Finish": ["2009-02-28", "2009-04-15", "2009-05-30"],
580+
}
581+
df = constructor(data)
582+
df = (
583+
nw.from_native(df)
584+
.with_columns(nw.col(datetime_columns).str.to_datetime(format="%Y-%m-%d"))
585+
.to_native()
586+
)
587+
fig = px.timeline(df, x_start="Start", x_end="Finish", y="Task", color="Task")
588+
assert len(fig.data) == 3
589+
assert fig.layout.xaxis.type == "date"
590+
assert fig.layout.xaxis.title.text is None

0 commit comments

Comments
 (0)
0