Skip to content

ENH: Add lazy copy for sort_index #50491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 3, 2023
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
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose this is a leftover from local testing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thanks for noticing

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, for testing, you can set the env variable PANDAS_COPY_ON_WRITE=1 pytest ..., which is of course a bit verbose. What I do locally is that I have an alias in my bashrc, so I can just do COW pytest ..., and then it is quite easy to switch between COW pytest ... or pytest ... when I want to run a specific test while developing with both COW enabled or not.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah agreed, did this when running tests in PyCharm

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I can't speak for PyCharm ;)

return pd.options.mode.copy_on_write and pd.options.mode.data_manager == "block"


Expand Down
2 changes: 1 addition & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4934,7 +4934,7 @@ def sort_index(
if inplace:
result = self
else:
result = self.copy()
result = self.copy(deep=None)

if ignore_index:
result.index = default_index(len(self))
Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/copy_view/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,23 @@ def test_reindex_like(using_copy_on_write):
tm.assert_frame_equal(df, df_orig)


def test_sort_index(using_copy_on_write):
# GH 49473
ser = Series([1, 2, 3])
ser_orig = ser.copy()
ser2 = ser.sort_index()

if using_copy_on_write:
assert np.shares_memory(ser.values, ser2.values)
else:
assert not np.shares_memory(ser.values, ser2.values)

# mutating ser triggers a copy-on-write for the column / block
ser2.iloc[0] = 0
assert not np.shares_memory(ser2.values, ser.values)
tm.assert_series_equal(ser, ser_orig)


def test_reorder_levels(using_copy_on_write):
index = MultiIndex.from_tuples(
[(1, 1), (1, 2), (2, 1), (2, 2)], names=["one", "two"]
Expand Down