From c342e7ad6aa6c31793a374dc7f3e8aad01399cad Mon Sep 17 00:00:00 2001 From: alexander-beedie Date: Wed, 26 Jul 2023 11:48:57 +0400 Subject: [PATCH] fix(python): address an inadvertently shallow-copy issue on underlying PySeries --- py-polars/polars/utils/_construction.py | 4 ++-- py-polars/tests/unit/operations/test_is_in.py | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/py-polars/polars/utils/_construction.py b/py-polars/polars/utils/_construction.py index 2a7222f29530..832e9f04b4e3 100644 --- a/py-polars/polars/utils/_construction.py +++ b/py-polars/polars/utils/_construction.py @@ -154,8 +154,8 @@ def nt_unpack(obj: Any) -> Any: def series_to_pyseries(name: str, values: Series) -> PySeries: - """Construct a PySeries from a Polars Series.""" - py_s = values._s + """Construct a new PySeries from a Polars Series.""" + py_s = values._s.clone() py_s.rename(name) return py_s diff --git a/py-polars/tests/unit/operations/test_is_in.py b/py-polars/tests/unit/operations/test_is_in.py index 8d283e4d8a6f..c262bd70b63d 100644 --- a/py-polars/tests/unit/operations/test_is_in.py +++ b/py-polars/tests/unit/operations/test_is_in.py @@ -5,6 +5,7 @@ import pytest import polars as pl +from polars.testing import assert_series_equal def test_struct_logical_is_in() -> None: @@ -101,3 +102,10 @@ def test_is_in_series() -> None: with pytest.raises(pl.ComputeError, match=r"cannot compare"): df.select(pl.col("b").is_in(["x", "x"])) + + # check we don't shallow-copy and accidentally modify 'a' (see: #10072) + a = pl.Series("a", [1, 2]) + b = pl.Series("b", [1, 3]).is_in(a) + + assert a.name == "a" + assert_series_equal(b, pl.Series("b", [True, False]))