Skip to content

fix: pandas DataFrame with non-default Index was resulting in scrambled data in scatter_matrix #4921

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2024
Merged
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
17 changes: 7 additions & 10 deletions packages/python/plotly/plotly/express/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1168,16 +1168,13 @@ def _escape_col_name(columns, col_name, extra):
return col_name


def to_unindexed_series(x, name=None, native_namespace=None):
"""Assuming x is list-like or even an existing Series, returns a new Series (with
its index reset if pandas-like). Stripping the index from existing pd.Series is
required to get things to match up right in the new DataFrame we're building.
"""
def to_named_series(x, name=None, native_namespace=None):
"""Assuming x is list-like or even an existing Series, returns a new Series named `name`."""
# With `pass_through=True`, the original object will be returned if unable to convert
# to a Narwhals Series.
x = nw.from_native(x, series_only=True, pass_through=True)
if isinstance(x, nw.Series):
return nw.maybe_reset_index(x).rename(name)
return x.rename(name)
elif native_namespace is not None:
return nw.new_series(name=name, values=x, native_namespace=native_namespace)
else:
Expand Down Expand Up @@ -1306,7 +1303,7 @@ def process_args_into_dataframe(
length,
)
)
df_output[col_name] = to_unindexed_series(
df_output[col_name] = to_named_series(
real_argument, col_name, native_namespace
)
elif not df_provided:
Expand Down Expand Up @@ -1343,7 +1340,7 @@ def process_args_into_dataframe(
)
else:
col_name = str(argument)
df_output[col_name] = to_unindexed_series(
df_output[col_name] = to_named_series(
df_input.get_column(argument), col_name
)
# ----------------- argument is likely a column / array / list.... -------
Expand All @@ -1362,7 +1359,7 @@ def process_args_into_dataframe(
argument.name is not None
and argument.name in df_input.columns
and (
to_unindexed_series(
to_named_series(
argument, argument.name, native_namespace
)
== df_input.get_column(argument.name)
Expand All @@ -1380,7 +1377,7 @@ def process_args_into_dataframe(
% (field, len_arg, str(list(df_output.keys())), length)
)

df_output[str(col_name)] = to_unindexed_series(
df_output[str(col_name)] = to_named_series(
x=argument,
name=str(col_name),
native_namespace=native_namespace,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,29 @@ def test_splom_case(backend):
assert np.all(fig.data[0].dimensions[0].values == ar[:, 0])


def test_scatter_matrix_indexed_pandas():
# https://github.com/plotly/plotly.py/issues/4917
# https://github.com/plotly/plotly.py/issues/4788
df = pd.DataFrame(
{
"x": [1, 2, 3, 4],
"y": [10, 20, 10, 20],
"z": [-1, -2, -3, -4],
"color": [1, 2, 3, 4],
}
)
df.index = pd.DatetimeIndex(
[
"1/1/2020 10:00:00+00:00",
"2/1/2020 11:00:00+00:00",
"3/1/2020 10:00:00+00:00",
"4/1/2020 11:00:00+00:00",
]
)
fig = px.scatter_matrix(df, color="color")
assert np.all(fig.data[0].marker["color"] == np.array([1, 2, 3, 4]))


def test_int_col_names(constructor):
# DataFrame with int column names
lengths = constructor({"0": np.random.random(100)})
Expand Down
2 changes: 1 addition & 1 deletion packages/python/plotly/requires-install.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
###################################################

## dataframe agnostic layer ##
narwhals>=1.13.3
narwhals>=1.15.1
packaging
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
requests==2.25.1
pytest==7.4.4
narwhals>=1.13.3
narwhals>=1.15.1
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ kaleido
orjson==3.8.12
polars[timezone]
pyarrow
narwhals>=1.13.3
narwhals>=1.15.1
anywidget==0.9.13
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
requests==2.25.1
pytest==7.4.4
narwhals>=1.13.3
narwhals>=1.15.1
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ kaleido
orjson==3.8.12
polars[timezone]
pyarrow
narwhals>=1.13.3
narwhals>=1.15.1
anywidget==0.9.13
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
requests==2.25.1
pytest==7.4.4
narwhals>=1.13.3
narwhals>=1.15.1
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ kaleido
orjson==3.9.10
polars[timezone]
pyarrow
narwhals>=1.13.3
narwhals>=1.15.1
anywidget==0.9.13
jupyter-console==6.4.4
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ kaleido
orjson==3.9.10
polars[timezone]
pyarrow
narwhals>=1.13.3
narwhals>=1.15.1
anywidget==0.9.13
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ kaleido
orjson==3.9.10
polars[timezone]
pyarrow
narwhals>=1.13.3
narwhals>=1.15.1
anywidget==0.9.13
jupyter-console==6.4.4
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
requests==2.25.1
pytest==8.1.1
narwhals>=1.13.3
narwhals>=1.15.1
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ psutil==5.7.0
kaleido
polars[timezone]
pyarrow
narwhals>=1.13.3
narwhals>=1.15.1
anywidget==0.9.13
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
requests==2.25.1
pytest==6.2.3
narwhals>=1.13.3
narwhals>=1.15.1
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ kaleido
orjson==3.8.12
polars[timezone]
pyarrow
narwhals>=1.13.3
narwhals>=1.15.1
anywidget==0.9.13
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ vaex
pydantic<=1.10.11 # for vaex, see https://github.com/vaexio/vaex/issues/2384
polars[timezone]
pyarrow
narwhals>=1.13.3
narwhals>=1.15.1
polars
anywidget==0.9.13