Skip to content

Backport PR #42449 on branch 1.3.x (REGR: DataFrame.agg with axis=1, EA dtype, and duplicate index) #42460

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
3 changes: 2 additions & 1 deletion doc/source/whatsnew/v1.3.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Fixed regressions
- Pandas could not be built on PyPy (:issue:`42355`)
- :class:`DataFrame` constructed with with an older version of pandas could not be unpickled (:issue:`42345`)
- Performance regression in constructing a :class:`DataFrame` from a dictionary of dictionaries (:issue:`42338`)
- Fixed regression in :meth:`DataFrame.agg` dropping values when the DataFrame had an Extension Array dtype, a duplicate index, and ``axis=1`` (:issue:`42380`)
-

.. ---------------------------------------------------------------------------
Expand All @@ -25,7 +26,7 @@ Fixed regressions

Bug fixes
~~~~~~~~~
-
- Fixed bug in :meth:`DataFrame.transpose` dropping values when the DataFrame had an Extension Array dtype and a duplicate index (:issue:`42380`)
-

.. ---------------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3344,8 +3344,8 @@ def transpose(self, *args, copy: bool = False) -> DataFrame:
values = self.values

new_values = [arr_type._from_sequence(row, dtype=dtype) for row in values]
result = self._constructor(
dict(zip(self.index, new_values)), index=self.columns
result = type(self)._from_arrays(
new_values, index=self.columns, columns=self.index
)

else:
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/apply/test_frame_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ def test_apply_axis1_with_ea():
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize(
"data, dtype",
[(1, None), (1, CategoricalDtype([1])), (Timestamp("2013-01-01", tz="UTC"), None)],
)
def test_agg_axis1_duplicate_index(data, dtype):
# GH 42380
expected = DataFrame([[data], [data]], index=["a", "a"], dtype=dtype)
result = expected.agg(lambda x: x, axis=1)
tm.assert_frame_equal(result, expected)


def test_apply_mixed_datetimelike():
# mixed datetimelike
# GH 7778
Expand Down
29 changes: 29 additions & 0 deletions pandas/tests/base/test_transpose.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import numpy as np
import pytest

from pandas import (
CategoricalDtype,
DataFrame,
)
import pandas._testing as tm


Expand All @@ -25,3 +29,28 @@ def test_numpy_transpose(index_or_series_obj):

with pytest.raises(ValueError, match=msg):
np.transpose(obj, axes=1)


@pytest.mark.parametrize(
"data, transposed_data, index, columns, dtype",
[
([[1], [2]], [[1, 2]], ["a", "a"], ["b"], int),
([[1], [2]], [[1, 2]], ["a", "a"], ["b"], CategoricalDtype([1, 2])),
([[1, 2]], [[1], [2]], ["b"], ["a", "a"], int),
([[1, 2]], [[1], [2]], ["b"], ["a", "a"], CategoricalDtype([1, 2])),
([[1, 2], [3, 4]], [[1, 3], [2, 4]], ["a", "a"], ["b", "b"], int),
(
[[1, 2], [3, 4]],
[[1, 3], [2, 4]],
["a", "a"],
["b", "b"],
CategoricalDtype([1, 2, 3, 4]),
),
],
)
def test_duplicate_labels(data, transposed_data, index, columns, dtype):
# GH 42380
df = DataFrame(data, index=index, columns=columns, dtype=dtype)
result = df.T
expected = DataFrame(transposed_data, index=columns, columns=index, dtype=dtype)
tm.assert_frame_equal(result, expected)