Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Adjust
  • Loading branch information
phofl committed Feb 17, 2023
commit ffcd4c055a04dbb9e9c2b4a4aa755b50b1ae57cc
9 changes: 3 additions & 6 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3557,17 +3557,14 @@ def transpose(self, *args, copy: bool = False) -> DataFrame:
if self._can_fast_transpose:
# Note: tests pass without this, but this improves perf quite a bit.
new_vals = self._values.T
if copy:
if copy and not using_copy_on_write():
new_vals = new_vals.copy()

result = self._constructor(
new_vals, index=self.columns, columns=self.index, copy=False
)
if using_copy_on_write() and not copy and len(self) > 0:
result._mgr.blocks[0].refs = self._mgr.blocks[0].refs # type: ignore[union-attr] # noqa
result._mgr.blocks[0].refs.add_reference( # type: ignore[union-attr]
result._mgr.blocks[0] # type: ignore[arg-type, union-attr]
)
if using_copy_on_write() and len(self) > 0:
result._mgr.add_references(self._mgr) # type: ignore[arg-type]

elif (
self._is_homogeneous_type and dtypes and is_extension_array_dtype(dtypes[0])
Expand Down
11 changes: 8 additions & 3 deletions pandas/tests/copy_view/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -1595,12 +1595,17 @@ def test_inplace_arithmetic_series_with_reference(using_copy_on_write):
assert np.shares_memory(get_array(ser), get_array(view))


def test_transpose(using_copy_on_write):
@pytest.mark.parametrize("copy", [True, False])
def test_transpose(using_copy_on_write, copy):
df = DataFrame({"a": [1, 2, 3], "b": 1})
df_orig = df.copy()
result = df.T
result = df.transpose(copy=copy)

if not copy or using_copy_on_write:
assert np.shares_memory(get_array(df, "a"), get_array(result, 0))
else:
assert not np.shares_memory(get_array(df, "a"), get_array(result, 0))

assert np.shares_memory(get_array(df, "a"), get_array(result, 0))
result.iloc[0, 0] = 100
if using_copy_on_write:
tm.assert_frame_equal(df, df_orig)
Expand Down