Skip to content

Commit 49ed9b3

Browse files
committed
fix: pandas DataFrame with non-default Index was resulting in scrambled
data in scatter_matrix
1 parent 8f50e2b commit 49ed9b3

16 files changed

+44
-24
lines changed

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,16 +1168,13 @@ def _escape_col_name(columns, col_name, extra):
11681168
return col_name
11691169

11701170

1171-
def to_unindexed_series(x, name=None, native_namespace=None):
1172-
"""Assuming x is list-like or even an existing Series, returns a new Series (with
1173-
its index reset if pandas-like). Stripping the index from existing pd.Series is
1174-
required to get things to match up right in the new DataFrame we're building.
1175-
"""
1171+
def to_named_series(x, name=None, native_namespace=None):
1172+
"""Assuming x is list-like or even an existing Series, returns a new Series named `name`."""
11761173
# With `pass_through=True`, the original object will be returned if unable to convert
11771174
# to a Narwhals Series.
11781175
x = nw.from_native(x, series_only=True, pass_through=True)
11791176
if isinstance(x, nw.Series):
1180-
return nw.maybe_reset_index(x).rename(name)
1177+
return x.rename(name)
11811178
elif native_namespace is not None:
11821179
return nw.new_series(name=name, values=x, native_namespace=native_namespace)
11831180
else:
@@ -1306,7 +1303,7 @@ def process_args_into_dataframe(
13061303
length,
13071304
)
13081305
)
1309-
df_output[col_name] = to_unindexed_series(
1306+
df_output[col_name] = to_named_series(
13101307
real_argument, col_name, native_namespace
13111308
)
13121309
elif not df_provided:
@@ -1343,7 +1340,7 @@ def process_args_into_dataframe(
13431340
)
13441341
else:
13451342
col_name = str(argument)
1346-
df_output[col_name] = to_unindexed_series(
1343+
df_output[col_name] = to_named_series(
13471344
df_input.get_column(argument), col_name
13481345
)
13491346
# ----------------- argument is likely a column / array / list.... -------
@@ -1362,7 +1359,7 @@ def process_args_into_dataframe(
13621359
argument.name is not None
13631360
and argument.name in df_input.columns
13641361
and (
1365-
to_unindexed_series(
1362+
to_named_series(
13661363
argument, argument.name, native_namespace
13671364
)
13681365
== df_input.get_column(argument.name)
@@ -1380,7 +1377,7 @@ def process_args_into_dataframe(
13801377
% (field, len_arg, str(list(df_output.keys())), length)
13811378
)
13821379

1383-
df_output[str(col_name)] = to_unindexed_series(
1380+
df_output[str(col_name)] = to_named_series(
13841381
x=argument,
13851382
name=str(col_name),
13861383
native_namespace=native_namespace,

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,29 @@ def test_splom_case(backend):
435435
assert np.all(fig.data[0].dimensions[0].values == ar[:, 0])
436436

437437

438+
def test_scatter_matrix_indexed_pandas():
439+
# https://github.com/plotly/plotly.py/issues/4917
440+
# https://github.com/plotly/plotly.py/issues/4788
441+
df = pd.DataFrame(
442+
{
443+
"x": [1, 2, 3, 4],
444+
"y": [10, 20, 10, 20],
445+
"z": [-1, -2, -3, -4],
446+
"color": [1, 2, 3, 4],
447+
}
448+
)
449+
df.index = pd.DatetimeIndex(
450+
[
451+
"1/1/2020 10:00:00+00:00",
452+
"2/1/2020 11:00:00+00:00",
453+
"3/1/2020 10:00:00+00:00",
454+
"4/1/2020 11:00:00+00:00",
455+
]
456+
)
457+
fig = px.scatter_matrix(df, color="color")
458+
assert np.all(fig.data[0].marker["color"] == np.array([1, 2, 3, 4]))
459+
460+
438461
def test_int_col_names(constructor):
439462
# DataFrame with int column names
440463
lengths = constructor({"0": np.random.random(100)})

packages/python/plotly/requires-install.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
###################################################
77

88
## dataframe agnostic layer ##
9-
narwhals>=1.13.3
9+
narwhals>=1.15.1
1010
packaging
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
requests==2.25.1
22
pytest==7.4.4
3-
narwhals>=1.13.3
3+
narwhals>=1.15.1

packages/python/plotly/test_requirements/requirements_310_optional.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ kaleido
2020
orjson==3.8.12
2121
polars[timezone]
2222
pyarrow
23-
narwhals>=1.13.3
23+
narwhals>=1.15.1
2424
anywidget==0.9.13
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
requests==2.25.1
22
pytest==7.4.4
3-
narwhals>=1.13.3
3+
narwhals>=1.15.1

packages/python/plotly/test_requirements/requirements_311_optional.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ kaleido
2020
orjson==3.8.12
2121
polars[timezone]
2222
pyarrow
23-
narwhals>=1.13.3
23+
narwhals>=1.15.1
2424
anywidget==0.9.13
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
requests==2.25.1
22
pytest==7.4.4
3-
narwhals>=1.13.3
3+
narwhals>=1.15.1

packages/python/plotly/test_requirements/requirements_312_no_numpy_optional.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ kaleido
1919
orjson==3.9.10
2020
polars[timezone]
2121
pyarrow
22-
narwhals>=1.13.3
22+
narwhals>=1.15.1
2323
anywidget==0.9.13
2424
jupyter-console==6.4.4

packages/python/plotly/test_requirements/requirements_312_np2_optional.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ kaleido
2121
orjson==3.9.10
2222
polars[timezone]
2323
pyarrow
24-
narwhals>=1.13.3
24+
narwhals>=1.15.1
2525
anywidget==0.9.13

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy