Skip to content

Commit

Permalink
BUG: swapaxes creating result with read_only array (#51945)
Browse files Browse the repository at this point in the history
  • Loading branch information
phofl authored Mar 14, 2023
1 parent dba0f66 commit ffc55a0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ def swapaxes(
mapping = {i: j, j: i}

new_axes = [self._get_axis(mapping.get(k, k)) for k in range(self._AXIS_LEN)]
new_values = self.values.swapaxes(i, j)
new_values = self._values.swapaxes(i, j) # type: ignore[union-attr]
if (
using_copy_on_write()
and self._mgr.is_single_block
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/copy_view/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,14 @@ def test_swapaxes_single_block(using_copy_on_write):
tm.assert_frame_equal(df, df_orig)


def test_swapaxes_read_only_array():
df = DataFrame({"a": [1, 2], "b": 3})
df = df.swapaxes(axis1="index", axis2="columns")
df.iloc[0, 0] = 100
expected = DataFrame({0: [100, 3], 1: [2, 3]}, index=["a", "b"])
tm.assert_frame_equal(df, expected)


@pytest.mark.parametrize(
"method, idx",
[
Expand Down

0 comments on commit ffc55a0

Please sign in to comment.