Skip to content

ENH: Add lazy copy for series.reorder_levels #50472

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 all commits
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
2 changes: 1 addition & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -4114,7 +4114,7 @@ def reorder_levels(self, order: Sequence[Level]) -> Series:
if not isinstance(self.index, MultiIndex): # pragma: no cover
raise Exception("Can only reorder levels on a hierarchical axis.")

result = self.copy()
result = self.copy(deep=None)
assert isinstance(result.index, MultiIndex)
result.index = result.index.reorder_levels(order)
return result
Expand Down
63 changes: 41 additions & 22 deletions pandas/tests/copy_view/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,25 @@ def test_reorder_levels(using_copy_on_write):
tm.assert_frame_equal(df, df_orig)


def test_series_reorder_levels(using_copy_on_write):
index = MultiIndex.from_tuples(
[(1, 1), (1, 2), (2, 1), (2, 2)], names=["one", "two"]
)
ser = Series([1, 2, 3, 4], index=index)
ser_orig = ser.copy()
ser2 = ser.reorder_levels(order=["two", "one"])

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

ser2.iloc[0] = 0
if using_copy_on_write:
assert not np.shares_memory(ser2.values, ser.values)
tm.assert_series_equal(ser, ser_orig)


@pytest.mark.parametrize("obj", [Series([1, 2, 3]), DataFrame({"a": [1, 2, 3]})])
def test_swaplevel(using_copy_on_write, obj):
index = MultiIndex.from_tuples([(1, 1), (1, 2), (2, 1)], names=["one", "two"])
Expand Down Expand Up @@ -461,28 +480,6 @@ def test_frame_set_axis(using_copy_on_write):
tm.assert_frame_equal(df, df_orig)


@pytest.mark.parametrize(
Copy link
Member

Choose a reason for hiding this comment

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

While fixing the merge conflict, also moved this test down, as it was now placed in between the related test_frame_set_axis and test_series_set_axis

"func, tz", [("tz_convert", "Europe/Berlin"), ("tz_localize", None)]
)
def test_tz_convert_localize(using_copy_on_write, func, tz):
# GH 49473
ser = Series(
[1, 2], index=date_range(start="2014-08-01 09:00", freq="H", periods=2, tz=tz)
)
ser_orig = ser.copy()
ser2 = getattr(ser, func)("US/Central")

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_series_set_axis(using_copy_on_write):
# GH 49473
ser = Series([1, 2, 3])
Expand Down Expand Up @@ -516,3 +513,25 @@ def test_rename_axis(using_copy_on_write, kwargs, copy_kwargs):
if using_copy_on_write:
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
tm.assert_frame_equal(df, df_orig)


@pytest.mark.parametrize(
"func, tz", [("tz_convert", "Europe/Berlin"), ("tz_localize", None)]
)
def test_tz_convert_localize(using_copy_on_write, func, tz):
# GH 49473
ser = Series(
[1, 2], index=date_range(start="2014-08-01 09:00", freq="H", periods=2, tz=tz)
)
ser_orig = ser.copy()
ser2 = getattr(ser, func)("US/Central")

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)