Skip to content
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
4 changes: 2 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -7407,7 +7407,7 @@ def swaplevel(self, i: Axis = -2, j: Axis = -1, axis: Axis = 0) -> DataFrame:
result.columns = result.columns.swaplevel(i, j)
return result

def reorder_levels(self, order: Sequence[Axis], axis: Axis = 0) -> DataFrame:
def reorder_levels(self, order: Sequence[int | str], axis: Axis = 0) -> DataFrame:
"""
Rearrange index levels using input order. May not drop or duplicate levels.

Expand Down Expand Up @@ -7452,7 +7452,7 @@ class diet
if not isinstance(self._get_axis(axis), MultiIndex): # pragma: no cover
raise TypeError("Can only reorder levels on a hierarchical axis.")

result = self.copy()
result = self.copy(deep=None)

if axis == 0:
assert isinstance(result.index, MultiIndex)
Expand Down
21 changes: 20 additions & 1 deletion pandas/tests/copy_view/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from pandas import (
DataFrame,
MultiIndex,
Series,
)
import pandas._testing as tm
Expand Down Expand Up @@ -293,7 +294,25 @@ def test_assign(using_copy_on_write):
else:
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))

# modify df2 to trigger CoW for that block
df2.iloc[0, 0] = 0
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)


def test_reorder_levels(using_copy_on_write):
index = MultiIndex.from_tuples(
[(1, 1), (1, 2), (2, 1), (2, 2)], names=["one", "two"]
)
df = DataFrame({"a": [1, 2, 3, 4]}, index=index)
df_orig = df.copy()
df2 = df.reorder_levels(order=["two", "one"])

if using_copy_on_write:
assert np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
else:
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))

df2.iloc[0, 0] = 0
if using_copy_on_write:
assert not np.shares_memory(get_array(df2, "a"), get_array(df, "a"))
Expand Down