Skip to content

Commit

Permalink
Backport PR #42449: REGR: DataFrame.agg with axis=1, EA dtype, and du…
Browse files Browse the repository at this point in the history
…plicate index (#42460)

Co-authored-by: Richard Shadrach <45562402+rhshadrach@users.noreply.github.com>
  • Loading branch information
meeseeksmachine and rhshadrach authored Jul 9, 2021
1 parent 456aa5c commit 3bcbe9b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
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)

0 comments on commit 3bcbe9b

Please sign in to comment.