Skip to content

Commit 4556fe2

Browse files
authored
BUG: pivot_table raising TypeError with ea dtype and dropna True (#47972)
1 parent dc229e6 commit 4556fe2

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

doc/source/whatsnew/v1.5.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,7 @@ Reshaping
10731073
- Bug in :func:`concat` losing dtype of columns when ``join="outer"`` and ``sort=True`` (:issue:`47329`)
10741074
- Bug in :func:`concat` not sorting the column names when ``None`` is included (:issue:`47331`)
10751075
- Bug in :func:`concat` with identical key leads to error when indexing :class:`MultiIndex` (:issue:`46519`)
1076+
- Bug in :func:`pivot_table` raising ``TypeError`` when ``dropna=True`` and aggregation column has extension array dtype (:issue:`47477`)
10761077
- Bug in :meth:`DataFrame.join` with a list when using suffixes to join DataFrames with duplicate column names (:issue:`46396`)
10771078
- Bug in :meth:`DataFrame.pivot_table` with ``sort=False`` results in sorted index (:issue:`17041`)
10781079
- Bug in :meth:`concat` when ``axis=1`` and ``sort=False`` where the resulting Index was a :class:`Int64Index` instead of a :class:`RangeIndex` (:issue:`46675`)

pandas/core/reshape/pivot.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ def __internal_pivot_table(
178178
and v in agged
179179
and not is_integer_dtype(agged[v])
180180
):
181-
if not isinstance(agged[v], ABCDataFrame):
181+
if not isinstance(agged[v], ABCDataFrame) and isinstance(
182+
data[v].dtype, np.dtype
183+
):
182184
# exclude DataFrame case bc maybe_downcast_to_dtype expects
183185
# ArrayLike
184186
# e.g. test_pivot_table_multiindex_columns_doctest_case

pandas/tests/reshape/test_pivot.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2223,6 +2223,21 @@ def test_pivot_table_with_margins_and_numeric_columns(self):
22232223

22242224
tm.assert_frame_equal(result, expected)
22252225

2226+
@pytest.mark.parametrize("dropna", [True, False])
2227+
def test_pivot_ea_dtype_dropna(self, dropna):
2228+
# GH#47477
2229+
df = DataFrame({"x": "a", "y": "b", "age": Series([20, 40], dtype="Int64")})
2230+
result = df.pivot_table(
2231+
index="x", columns="y", values="age", aggfunc="mean", dropna=dropna
2232+
)
2233+
expected = DataFrame(
2234+
[[30]],
2235+
index=Index(["a"], name="x"),
2236+
columns=Index(["b"], name="y"),
2237+
dtype="Float64",
2238+
)
2239+
tm.assert_frame_equal(result, expected)
2240+
22262241

22272242
class TestPivot:
22282243
def test_pivot(self):

0 commit comments

Comments
 (0)