Skip to content

Commit fc80646

Browse files
mabelvjproost
authored andcommitted
BUG: pivot_table not returning correct type when margin=True and aggfunc='mean' (pandas-dev#28248)
1 parent 7445252 commit fc80646

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

doc/source/whatsnew/v1.0.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,7 @@ Reshaping
595595

596596
- Bug in :meth:`DataFrame.apply` that caused incorrect output with empty :class:`DataFrame` (:issue:`28202`, :issue:`21959`)
597597
- Bug in :meth:`DataFrame.stack` not handling non-unique indexes correctly when creating MultiIndex (:issue: `28301`)
598+
- Bug in :meth:`pivot_table` not returning correct type ``float`` when ``margins=True`` and ``aggfunc='mean'`` (:issue:`24893`)
598599
- Bug :func:`merge_asof` could not use :class:`datetime.timedelta` for ``tolerance`` kwarg (:issue:`28098`)
599600
- Bug in :func:`merge`, did not append suffixes correctly with MultiIndex (:issue:`28518`)
600601
- :func:`qcut` and :func:`cut` now handle boolean input (:issue:`20303`)
@@ -604,6 +605,7 @@ Reshaping
604605
- Bug where :meth:`DataFrame.equals` returned True incorrectly in some cases when two DataFrames had the same columns in different orders (:issue:`28839`)
605606
- Bug in :meth:`DataFrame.replace` that caused non-numeric replacer's dtype not respected (:issue:`26632`)
606607

608+
607609
Sparse
608610
^^^^^^
609611
- Bug in :class:`SparseDataFrame` arithmetic operations incorrectly casting inputs to float (:issue:`28107`)

pandas/core/reshape/pivot.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,12 @@ def _add_margins(
261261

262262
row_names = result.index.names
263263
try:
264+
# check the result column and leave floats
264265
for dtype in set(result.dtypes):
265266
cols = result.select_dtypes([dtype]).columns
266-
margin_dummy[cols] = margin_dummy[cols].astype(dtype)
267+
margin_dummy[cols] = margin_dummy[cols].apply(
268+
maybe_downcast_to_dtype, args=(dtype,)
269+
)
267270
result = result.append(margin_dummy)
268271
except TypeError:
269272

pandas/tests/reshape/merge/test_pivot_old.py

Whitespace-only changes.

pandas/tests/reshape/test_pivot.py

+18
Original file line numberDiff line numberDiff line change
@@ -1656,6 +1656,24 @@ def test_categorical_margins_category(self, observed):
16561656
table = df.pivot_table("x", "y", "z", dropna=observed, margins=True)
16571657
tm.assert_frame_equal(table, expected)
16581658

1659+
def test_margins_casted_to_float(self, observed):
1660+
# GH 24893
1661+
df = pd.DataFrame(
1662+
{
1663+
"A": [2, 4, 6, 8],
1664+
"B": [1, 4, 5, 8],
1665+
"C": [1, 3, 4, 6],
1666+
"D": ["X", "X", "Y", "Y"],
1667+
}
1668+
)
1669+
1670+
result = pd.pivot_table(df, index="D", margins=True)
1671+
expected = pd.DataFrame(
1672+
{"A": [3, 7, 5], "B": [2.5, 6.5, 4.5], "C": [2, 5, 3.5]},
1673+
index=pd.Index(["X", "Y", "All"], name="D"),
1674+
)
1675+
tm.assert_frame_equal(result, expected)
1676+
16591677
def test_pivot_with_categorical(self, observed, ordered_fixture):
16601678
# gh-21370
16611679
idx = [np.nan, "low", "high", "low", np.nan]

0 commit comments

Comments
 (0)