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
Next Next commit
TST: Test cow for set_flags
  • Loading branch information
phofl committed Dec 29, 2022
commit 83edb2926a7d34165236f544cfc4b32ec6157b59
1 change: 1 addition & 0 deletions pandas/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1903,6 +1903,7 @@ def using_copy_on_write() -> bool:
"""
Fixture to check if Copy-on-Write is enabled.
"""
pd.options.mode.copy_on_write = True
return pd.options.mode.copy_on_write and pd.options.mode.data_manager == "block"


Expand Down
2 changes: 2 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,8 @@ def set_flags(

Parameters
----------
copy : bool, default False
Specify if a copy of the object should be made.
allows_duplicate_labels : bool, optional
Whether the returned object allows duplicate labels.

Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/copy_view/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,3 +456,21 @@ def test_series_set_axis(using_copy_on_write):
ser2.iloc[0] = 0
assert not np.shares_memory(ser2, ser)
tm.assert_series_equal(ser, ser_orig)


def test_set_flags(using_copy_on_write):
ser = Series([1, 2, 3])
ser_orig = ser.copy()
ser2 = ser.set_flags(allows_duplicate_labels=False)

assert np.shares_memory(ser, ser2)

# mutating ser triggers a copy-on-write for the column / block
ser2.iloc[0] = 0
if using_copy_on_write:
assert not np.shares_memory(ser2, ser)
tm.assert_series_equal(ser, ser_orig)
else:
assert np.shares_memory(ser2, ser)
expected = Series([0, 2, 3])
tm.assert_series_equal(ser, expected)