Skip to content

Commit 819413f

Browse files
committed
CoW: Add warning for slicing a Series with a MultiIndex
1 parent 4c520e3 commit 819413f

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

pandas/core/series.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,7 @@ def _get_value(self, label, takeable: bool = False):
12131213
new_ser = self._constructor(
12141214
new_values, index=new_index, name=self.name, copy=False
12151215
)
1216-
if using_copy_on_write() and isinstance(loc, slice):
1216+
if isinstance(loc, slice):
12171217
new_ser._mgr.add_references(self._mgr) # type: ignore[arg-type]
12181218
return new_ser.__finalize__(self)
12191219

pandas/tests/copy_view/test_indexing.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1150,13 +1150,12 @@ def test_set_value_copy_only_necessary_column(
11501150
assert np.shares_memory(get_array(df, "a"), get_array(view, "a"))
11511151

11521152

1153-
def test_series_midx_slice(using_copy_on_write):
1153+
def test_series_midx_slice(using_copy_on_write, warn_copy_on_write):
11541154
ser = Series([1, 2, 3], index=pd.MultiIndex.from_arrays([[1, 1, 2], [3, 4, 5]]))
11551155
result = ser[1]
11561156
assert np.shares_memory(get_array(ser), get_array(result))
1157-
# TODO(CoW-warn) should warn -> reference is only tracked in CoW mode, so
1158-
# warning is not triggered
1159-
result.iloc[0] = 100
1157+
with tm.assert_cow_warning(warn_copy_on_write):
1158+
result.iloc[0] = 100
11601159
if using_copy_on_write:
11611160
expected = Series(
11621161
[1, 2, 3], index=pd.MultiIndex.from_arrays([[1, 1, 2], [3, 4, 5]])

0 commit comments

Comments
 (0)