Skip to content

Commit 2d6b373

Browse files
committed
REGR: DataFrame.transpose resulting in not contiguous data on nullable EAs
1 parent 92a52e2 commit 2d6b373

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

doc/source/whatsnew/v2.2.1.rst

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Fixed regressions
3030
- Fixed regression in :meth:`DataFrame.sort_index` not producing a stable sort for a index with duplicates (:issue:`57151`)
3131
- Fixed regression in :meth:`DataFrame.to_dict` with ``orient='list'`` and datetime or timedelta types returning integers (:issue:`54824`)
3232
- Fixed regression in :meth:`DataFrame.to_json` converting nullable integers to floats (:issue:`57224`)
33+
- Fixed regression in :meth:`DataFrame.transpose` with nullable extension dtypes not having F-contiguous data potentially causing exceptions when used (:issue:`57315`)
3334
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` ignoring the ``skipna`` argument (:issue:`57040`)
3435
- Fixed regression in :meth:`DataFrameGroupBy.idxmin`, :meth:`DataFrameGroupBy.idxmax`, :meth:`SeriesGroupBy.idxmin`, :meth:`SeriesGroupBy.idxmax` where values containing the minimum or maximum value for the dtype could produce incorrect results (:issue:`57040`)
3536
- Fixed regression in :meth:`ExtensionArray.to_numpy` raising for non-numeric masked dtypes (:issue:`56991`)

pandas/core/arrays/masked.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1661,7 +1661,9 @@ def transpose_homogeneous_masked_arrays(
16611661
arr_type = dtype.construct_array_type()
16621662
transposed_arrays: list[BaseMaskedArray] = []
16631663
for i in range(transposed_values.shape[1]):
1664-
transposed_arr = arr_type(transposed_values[:, i], mask=transposed_masks[:, i])
1664+
transposed_arr = arr_type(
1665+
transposed_values[:, i].copy(), mask=transposed_masks[:, i].copy()
1666+
)
16651667
transposed_arrays.append(transposed_arr)
16661668

16671669
return transposed_arrays

pandas/tests/frame/methods/test_transpose.py

+17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import numpy as np
22
import pytest
33

4+
import pandas as pd
45
from pandas import (
56
DataFrame,
67
DatetimeIndex,
@@ -179,3 +180,19 @@ def test_transpose_not_inferring_dt_mixed_blocks(self):
179180
dtype=object,
180181
)
181182
tm.assert_frame_equal(result, expected)
183+
184+
@pytest.mark.parametrize("dtype1", ["Int64", "Float64"])
185+
@pytest.mark.parametrize("dtype2", ["Int64", "Float64"])
186+
def test_transpose(self, dtype1, dtype2):
187+
# GH#57315 - transpose should have C/F contiguous blocks
188+
df = DataFrame(
189+
{
190+
"a": pd.array([1, 1, 2], dtype=dtype1),
191+
"b": pd.array([3, 4, 5], dtype=dtype2),
192+
}
193+
)
194+
result = df.T
195+
for blk in result._mgr.blocks:
196+
# When dtypes are unequal, we get NumPy object array
197+
data = blk.values._data if dtype1 == dtype2 else blk.values
198+
assert data.flags["F_CONTIGUOUS"]

0 commit comments

Comments
 (0)