Skip to content

Commit dd7c34f

Browse files
CoW: Add warning for slicing a Series with a MultiIndex (#56214)
Co-authored-by: Joris Van den Bossche <[email protected]>
1 parent 9211826 commit dd7c34f

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

pandas/core/series.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1175,7 +1175,7 @@ def _get_values_tuple(self, key: tuple):
11751175
# If key is contained, would have returned by now
11761176
indexer, new_index = self.index.get_loc_level(key)
11771177
new_ser = self._constructor(self._values[indexer], index=new_index, copy=False)
1178-
if using_copy_on_write() and isinstance(indexer, slice):
1178+
if isinstance(indexer, slice):
11791179
new_ser._mgr.add_references(self._mgr) # type: ignore[arg-type]
11801180
return new_ser.__finalize__(self)
11811181

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

pandas/tests/copy_view/test_indexing.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -1141,16 +1141,18 @@ def test_set_value_copy_only_necessary_column(
11411141
assert np.shares_memory(get_array(df, "a"), get_array(view, "a"))
11421142

11431143

1144-
def test_series_midx_slice(using_copy_on_write):
1144+
def test_series_midx_slice(using_copy_on_write, warn_copy_on_write):
11451145
ser = Series([1, 2, 3], index=pd.MultiIndex.from_arrays([[1, 1, 2], [3, 4, 5]]))
1146+
ser_orig = ser.copy()
11461147
result = ser[1]
11471148
assert np.shares_memory(get_array(ser), get_array(result))
1148-
# TODO(CoW-warn) should warn -> reference is only tracked in CoW mode, so
1149-
# warning is not triggered
1150-
result.iloc[0] = 100
1149+
with tm.assert_cow_warning(warn_copy_on_write):
1150+
result.iloc[0] = 100
11511151
if using_copy_on_write:
1152+
tm.assert_series_equal(ser, ser_orig)
1153+
else:
11521154
expected = Series(
1153-
[1, 2, 3], index=pd.MultiIndex.from_arrays([[1, 1, 2], [3, 4, 5]])
1155+
[100, 2, 3], index=pd.MultiIndex.from_arrays([[1, 1, 2], [3, 4, 5]])
11541156
)
11551157
tm.assert_series_equal(ser, expected)
11561158

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

11831185

1184-
def test_series_midx_tuples_slice(using_copy_on_write):
1186+
def test_series_midx_tuples_slice(using_copy_on_write, warn_copy_on_write):
11851187
ser = Series(
11861188
[1, 2, 3],
11871189
index=pd.MultiIndex.from_tuples([((1, 2), 3), ((1, 2), 4), ((2, 3), 4)]),
11881190
)
11891191
result = ser[(1, 2)]
11901192
assert np.shares_memory(get_array(ser), get_array(result))
1191-
# TODO(CoW-warn) should warn -> reference is only tracked in CoW mode, so
1192-
# warning is not triggered
1193-
result.iloc[0] = 100
1193+
with tm.assert_cow_warning(warn_copy_on_write):
1194+
result.iloc[0] = 100
11941195
if using_copy_on_write:
11951196
expected = Series(
11961197
[1, 2, 3],

0 commit comments

Comments
 (0)