Skip to content

CoW: Add warning for slicing a Series with a MultiIndex #56214

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 6 commits into from
Dec 4, 2023
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
4 changes: 2 additions & 2 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1175,7 +1175,7 @@ def _get_values_tuple(self, key: tuple):
# If key is contained, would have returned by now
indexer, new_index = self.index.get_loc_level(key)
new_ser = self._constructor(self._values[indexer], index=new_index, copy=False)
if using_copy_on_write() and isinstance(indexer, slice):
if isinstance(indexer, slice):
new_ser._mgr.add_references(self._mgr) # type: ignore[arg-type]
return new_ser.__finalize__(self)

Expand Down Expand Up @@ -1217,7 +1217,7 @@ def _get_value(self, label, takeable: bool = False):
new_ser = self._constructor(
new_values, index=new_index, name=self.name, copy=False
)
if using_copy_on_write() and isinstance(loc, slice):
if isinstance(loc, slice):
new_ser._mgr.add_references(self._mgr) # type: ignore[arg-type]
return new_ser.__finalize__(self)

Expand Down
19 changes: 10 additions & 9 deletions pandas/tests/copy_view/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1141,16 +1141,18 @@ def test_set_value_copy_only_necessary_column(
assert np.shares_memory(get_array(df, "a"), get_array(view, "a"))


def test_series_midx_slice(using_copy_on_write):
def test_series_midx_slice(using_copy_on_write, warn_copy_on_write):
ser = Series([1, 2, 3], index=pd.MultiIndex.from_arrays([[1, 1, 2], [3, 4, 5]]))
ser_orig = ser.copy()
result = ser[1]
assert np.shares_memory(get_array(ser), get_array(result))
# TODO(CoW-warn) should warn -> reference is only tracked in CoW mode, so
# warning is not triggered
result.iloc[0] = 100
with tm.assert_cow_warning(warn_copy_on_write):
result.iloc[0] = 100
if using_copy_on_write:
tm.assert_series_equal(ser, ser_orig)
else:
expected = Series(
[1, 2, 3], index=pd.MultiIndex.from_arrays([[1, 1, 2], [3, 4, 5]])
[100, 2, 3], index=pd.MultiIndex.from_arrays([[1, 1, 2], [3, 4, 5]])
)
tm.assert_series_equal(ser, expected)

Expand Down Expand Up @@ -1181,16 +1183,15 @@ def test_getitem_midx_slice(
assert df.iloc[0, 0] == 100


def test_series_midx_tuples_slice(using_copy_on_write):
def test_series_midx_tuples_slice(using_copy_on_write, warn_copy_on_write):
ser = Series(
[1, 2, 3],
index=pd.MultiIndex.from_tuples([((1, 2), 3), ((1, 2), 4), ((2, 3), 4)]),
)
result = ser[(1, 2)]
assert np.shares_memory(get_array(ser), get_array(result))
# TODO(CoW-warn) should warn -> reference is only tracked in CoW mode, so
# warning is not triggered
result.iloc[0] = 100
with tm.assert_cow_warning(warn_copy_on_write):
result.iloc[0] = 100
if using_copy_on_write:
expected = Series(
[1, 2, 3],
Expand Down