diff --git a/pandas/core/series.py b/pandas/core/series.py index 42e41203cc31e..b5d73373f061e 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -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 diff --git a/pandas/tests/copy_view/test_methods.py b/pandas/tests/copy_view/test_methods.py index 2b3d13b982d4d..a0201a1e82000 100644 --- a/pandas/tests/copy_view/test_methods.py +++ b/pandas/tests/copy_view/test_methods.py @@ -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"]) @@ -461,28 +480,6 @@ def test_frame_set_axis(using_copy_on_write): 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) - - def test_series_set_axis(using_copy_on_write): # GH 49473 ser = Series([1, 2, 3]) @@ -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)