Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 4 additions & 3 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1044,9 +1044,10 @@ 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)
return self._constructor(self._values[indexer], index=new_index).__finalize__(
self
)
result = self._constructor(self._values[indexer], index=new_index)
if using_copy_on_write() and isinstance(indexer, slice):
result._mgr.add_references(self._mgr) # type: ignore[arg-type]
return result.__finalize__(self)

def _get_values(self, indexer: slice | npt.NDArray[np.bool_]) -> Series:
new_mgr = self._mgr.getitem_mgr(indexer)
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/copy_view/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1049,3 +1049,19 @@ def test_getitem_midx_slice(using_copy_on_write, using_array_manager):
if using_copy_on_write:
new_df.iloc[0, 0] = 100
tm.assert_frame_equal(df_orig, df)


def test_series_midx_tuples_slice(using_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))
result.iloc[0] = 100
if using_copy_on_write:
expected = Series(
[1, 2, 3],
index=pd.MultiIndex.from_tuples([((1, 2), 3), ((1, 2), 4), ((2, 3), 4)]),
)
tm.assert_series_equal(ser, expected)