From 49ed9b34f90876cee17c69008053d32f688755a5 Mon Sep 17 00:00:00 2001 From: Marco Gorelli <33491632+MarcoGorelli@users.noreply.github.com> Date: Sat, 30 Nov 2024 19:55:39 +0000 Subject: [PATCH] fix: pandas DataFrame with non-default Index was resulting in scrambled data in scatter_matrix --- .../python/plotly/plotly/express/_core.py | 17 ++++++-------- .../test_optional/test_px/test_px_input.py | 23 +++++++++++++++++++ packages/python/plotly/requires-install.txt | 2 +- .../requirements_310_core.txt | 2 +- .../requirements_310_optional.txt | 2 +- .../requirements_311_core.txt | 2 +- .../requirements_311_optional.txt | 2 +- .../requirements_312_core.txt | 2 +- .../requirements_312_no_numpy_optional.txt | 2 +- .../requirements_312_np2_optional.txt | 2 +- .../requirements_312_optional.txt | 2 +- .../requirements_38_core.txt | 2 +- .../requirements_38_optional.txt | 2 +- .../requirements_39_core.txt | 2 +- .../requirements_39_optional.txt | 2 +- .../requirements_39_pandas_2_optional.txt | 2 +- 16 files changed, 44 insertions(+), 24 deletions(-) diff --git a/packages/python/plotly/plotly/express/_core.py b/packages/python/plotly/plotly/express/_core.py index 5823f2defe4..5da419e5d53 100644 --- a/packages/python/plotly/plotly/express/_core.py +++ b/packages/python/plotly/plotly/express/_core.py @@ -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: @@ -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: @@ -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.... ------- @@ -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) @@ -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, diff --git a/packages/python/plotly/plotly/tests/test_optional/test_px/test_px_input.py b/packages/python/plotly/plotly/tests/test_optional/test_px/test_px_input.py index dff76b4863e..45325b7c5f5 100644 --- a/packages/python/plotly/plotly/tests/test_optional/test_px/test_px_input.py +++ b/packages/python/plotly/plotly/tests/test_optional/test_px/test_px_input.py @@ -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)}) diff --git a/packages/python/plotly/requires-install.txt b/packages/python/plotly/requires-install.txt index d452bad7d60..2923ec9d8ab 100644 --- a/packages/python/plotly/requires-install.txt +++ b/packages/python/plotly/requires-install.txt @@ -6,5 +6,5 @@ ################################################### ## dataframe agnostic layer ## -narwhals>=1.13.3 +narwhals>=1.15.1 packaging diff --git a/packages/python/plotly/test_requirements/requirements_310_core.txt b/packages/python/plotly/test_requirements/requirements_310_core.txt index 771df9cc87a..a9e44d7480f 100644 --- a/packages/python/plotly/test_requirements/requirements_310_core.txt +++ b/packages/python/plotly/test_requirements/requirements_310_core.txt @@ -1,3 +1,3 @@ requests==2.25.1 pytest==7.4.4 -narwhals>=1.13.3 +narwhals>=1.15.1 diff --git a/packages/python/plotly/test_requirements/requirements_310_optional.txt b/packages/python/plotly/test_requirements/requirements_310_optional.txt index 295d594fb5b..f61fef5a5dc 100644 --- a/packages/python/plotly/test_requirements/requirements_310_optional.txt +++ b/packages/python/plotly/test_requirements/requirements_310_optional.txt @@ -20,5 +20,5 @@ kaleido orjson==3.8.12 polars[timezone] pyarrow -narwhals>=1.13.3 +narwhals>=1.15.1 anywidget==0.9.13 diff --git a/packages/python/plotly/test_requirements/requirements_311_core.txt b/packages/python/plotly/test_requirements/requirements_311_core.txt index 938e282b0f0..18000388687 100644 --- a/packages/python/plotly/test_requirements/requirements_311_core.txt +++ b/packages/python/plotly/test_requirements/requirements_311_core.txt @@ -1,3 +1,3 @@ requests==2.25.1 pytest==7.4.4 -narwhals>=1.13.3 \ No newline at end of file +narwhals>=1.15.1 \ No newline at end of file diff --git a/packages/python/plotly/test_requirements/requirements_311_optional.txt b/packages/python/plotly/test_requirements/requirements_311_optional.txt index e18e6536384..19771444dcf 100644 --- a/packages/python/plotly/test_requirements/requirements_311_optional.txt +++ b/packages/python/plotly/test_requirements/requirements_311_optional.txt @@ -20,5 +20,5 @@ kaleido orjson==3.8.12 polars[timezone] pyarrow -narwhals>=1.13.3 +narwhals>=1.15.1 anywidget==0.9.13 diff --git a/packages/python/plotly/test_requirements/requirements_312_core.txt b/packages/python/plotly/test_requirements/requirements_312_core.txt index 771df9cc87a..a9e44d7480f 100644 --- a/packages/python/plotly/test_requirements/requirements_312_core.txt +++ b/packages/python/plotly/test_requirements/requirements_312_core.txt @@ -1,3 +1,3 @@ requests==2.25.1 pytest==7.4.4 -narwhals>=1.13.3 +narwhals>=1.15.1 diff --git a/packages/python/plotly/test_requirements/requirements_312_no_numpy_optional.txt b/packages/python/plotly/test_requirements/requirements_312_no_numpy_optional.txt index 79cfe14a7e2..9786aea5f6a 100644 --- a/packages/python/plotly/test_requirements/requirements_312_no_numpy_optional.txt +++ b/packages/python/plotly/test_requirements/requirements_312_no_numpy_optional.txt @@ -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 diff --git a/packages/python/plotly/test_requirements/requirements_312_np2_optional.txt b/packages/python/plotly/test_requirements/requirements_312_np2_optional.txt index 94e73ac11ae..1e02e3a8360 100644 --- a/packages/python/plotly/test_requirements/requirements_312_np2_optional.txt +++ b/packages/python/plotly/test_requirements/requirements_312_np2_optional.txt @@ -21,5 +21,5 @@ kaleido orjson==3.9.10 polars[timezone] pyarrow -narwhals>=1.13.3 +narwhals>=1.15.1 anywidget==0.9.13 diff --git a/packages/python/plotly/test_requirements/requirements_312_optional.txt b/packages/python/plotly/test_requirements/requirements_312_optional.txt index 3609e9b2725..15781ae5adb 100644 --- a/packages/python/plotly/test_requirements/requirements_312_optional.txt +++ b/packages/python/plotly/test_requirements/requirements_312_optional.txt @@ -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 diff --git a/packages/python/plotly/test_requirements/requirements_38_core.txt b/packages/python/plotly/test_requirements/requirements_38_core.txt index 2983296113c..9bfcbffa2ff 100644 --- a/packages/python/plotly/test_requirements/requirements_38_core.txt +++ b/packages/python/plotly/test_requirements/requirements_38_core.txt @@ -1,3 +1,3 @@ requests==2.25.1 pytest==8.1.1 -narwhals>=1.13.3 +narwhals>=1.15.1 diff --git a/packages/python/plotly/test_requirements/requirements_38_optional.txt b/packages/python/plotly/test_requirements/requirements_38_optional.txt index dc5cb4efe3c..f62c6ad6560 100644 --- a/packages/python/plotly/test_requirements/requirements_38_optional.txt +++ b/packages/python/plotly/test_requirements/requirements_38_optional.txt @@ -20,5 +20,5 @@ psutil==5.7.0 kaleido polars[timezone] pyarrow -narwhals>=1.13.3 +narwhals>=1.15.1 anywidget==0.9.13 diff --git a/packages/python/plotly/test_requirements/requirements_39_core.txt b/packages/python/plotly/test_requirements/requirements_39_core.txt index b6055de8091..0d36eb9460e 100644 --- a/packages/python/plotly/test_requirements/requirements_39_core.txt +++ b/packages/python/plotly/test_requirements/requirements_39_core.txt @@ -1,3 +1,3 @@ requests==2.25.1 pytest==6.2.3 -narwhals>=1.13.3 +narwhals>=1.15.1 diff --git a/packages/python/plotly/test_requirements/requirements_39_optional.txt b/packages/python/plotly/test_requirements/requirements_39_optional.txt index 153065dfd46..1a767fe4926 100644 --- a/packages/python/plotly/test_requirements/requirements_39_optional.txt +++ b/packages/python/plotly/test_requirements/requirements_39_optional.txt @@ -21,5 +21,5 @@ kaleido orjson==3.8.12 polars[timezone] pyarrow -narwhals>=1.13.3 +narwhals>=1.15.1 anywidget==0.9.13 diff --git a/packages/python/plotly/test_requirements/requirements_39_pandas_2_optional.txt b/packages/python/plotly/test_requirements/requirements_39_pandas_2_optional.txt index 995c8aec7cd..fdfe035dfe9 100644 --- a/packages/python/plotly/test_requirements/requirements_39_pandas_2_optional.txt +++ b/packages/python/plotly/test_requirements/requirements_39_pandas_2_optional.txt @@ -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