-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Add lazy copy for sort_values #50643
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
Changes from 7 commits
714f4f0
e5a8237
7385099
57ece3b
b60488e
06df224
b03750a
7382ac5
f950076
e2c1771
fe248bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,6 +1,7 @@ | ||||||||||||
import numpy as np | ||||||||||||
import pytest | ||||||||||||
|
||||||||||||
import pandas as pd | ||||||||||||
from pandas import ( | ||||||||||||
DataFrame, | ||||||||||||
Index, | ||||||||||||
|
@@ -575,6 +576,48 @@ def test_sort_index(using_copy_on_write): | |||||||||||
tm.assert_series_equal(ser, ser_orig) | ||||||||||||
|
||||||||||||
|
||||||||||||
@pytest.mark.parametrize( | ||||||||||||
"obj, kwargs", [(Series([1, 2, 3]), {}), (DataFrame({"a": [1, 2, 3]}), {"by": "a"})] | ||||||||||||
) | ||||||||||||
def test_sort_values(using_copy_on_write, obj, kwargs): | ||||||||||||
obj_orig = obj.copy() | ||||||||||||
obj2 = obj.sort_values(**kwargs) | ||||||||||||
|
||||||||||||
if using_copy_on_write: | ||||||||||||
assert np.shares_memory(obj.values, obj2.values) | ||||||||||||
else: | ||||||||||||
assert not np.shares_memory(obj.values, obj2.values) | ||||||||||||
|
||||||||||||
# mutating df triggers a copy-on-write for the column / block | ||||||||||||
obj2.iloc[0] = 0 | ||||||||||||
assert not np.shares_memory(obj2.values, obj.values) | ||||||||||||
tm.assert_equal(obj, obj_orig) | ||||||||||||
|
||||||||||||
|
||||||||||||
@pytest.mark.parametrize( | ||||||||||||
"obj, kwargs", [(Series([1, 2, 3]), {}), (DataFrame({"a": [1, 2, 3]}), {"by": "a"})] | ||||||||||||
) | ||||||||||||
def test_sort_values_inplace(using_copy_on_write, obj, kwargs, using_array_manager): | ||||||||||||
obj_orig = obj.copy() | ||||||||||||
view = obj[:] | ||||||||||||
obj.sort_values(inplace=True, **kwargs) | ||||||||||||
|
||||||||||||
assert np.shares_memory(obj.values, view.values) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, this is failing with ArrayManager (failing CI) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You will have to use the same pattern below, so can probably simplify this if we give the Series a name so can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While testing this, I just pushed the changes I was doing locally, hopefully you weren't yet working on it. |
||||||||||||
|
||||||||||||
# mutating obj triggers a copy-on-write for the column / block | ||||||||||||
obj.iloc[0] = 0 | ||||||||||||
if ( | ||||||||||||
using_copy_on_write | ||||||||||||
or using_array_manager | ||||||||||||
and isinstance(obj, DataFrame) | ||||||||||||
jorisvandenbossche marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
and pd.options.mode.copy_on_write | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ArrayManager never uses Copy-on-Write (we should maybe error when setting those two options at the same time), so this check (and the |
||||||||||||
): | ||||||||||||
assert not np.shares_memory(view.values, obj.values) | ||||||||||||
tm.assert_equal(view, obj_orig) | ||||||||||||
else: | ||||||||||||
assert np.shares_memory(view.values, obj.values) | ||||||||||||
|
||||||||||||
|
||||||||||||
def test_reorder_levels(using_copy_on_write): | ||||||||||||
index = MultiIndex.from_tuples( | ||||||||||||
[(1, 1), (1, 2), (2, 1), (2, 2)], names=["one", "two"] | ||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.