Skip to content

Commit

Permalink
BUG / CoW: ensure ser[...] returns new object
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisvandenbossche committed Dec 4, 2023
1 parent 5165102 commit 8683fb6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,8 @@ def __getitem__(self, key):
key = com.apply_if_callable(key, self)

if key is Ellipsis:
if using_copy_on_write() or warn_copy_on_write():
return self.copy(deep=False)
return self

key_is_scalar = is_scalar(key)
Expand Down
25 changes: 25 additions & 0 deletions pandas/tests/copy_view/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,31 @@ def test_series_getitem_slice(backend, using_copy_on_write, warn_copy_on_write):
assert s.iloc[0] == 0


def test_series_getitem_ellipsis(using_copy_on_write, warn_copy_on_write):
# Case: taking a view of a Series using Ellipsis + afterwards modifying the subset
s = Series([1, 2, 3])
s_orig = s.copy()

subset = s[...]
assert np.shares_memory(get_array(subset), get_array(s))

with tm.assert_cow_warning(warn_copy_on_write):
subset.iloc[0] = 0

if using_copy_on_write:
assert not np.shares_memory(get_array(subset), get_array(s))

expected = Series([0, 2, 3])
tm.assert_series_equal(subset, expected)

if using_copy_on_write:
# original parent series is not modified (CoW)
tm.assert_series_equal(s, s_orig)
else:
# original parent series is actually updated
assert s.iloc[0] == 0


@pytest.mark.parametrize(
"indexer",
[slice(0, 2), np.array([True, True, False]), np.array([0, 1])],
Expand Down

0 comments on commit 8683fb6

Please sign in to comment.