Skip to content

Commit 9e7cb7c

Browse files
authored
BUG: Fix DateFrameGroupBy.mean error for Int64 dtype (#32223)
1 parent 9bc3ee0 commit 9e7cb7c

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

doc/source/whatsnew/v1.0.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Bug fixes
9292
- Fixed bug in :meth:`DataFrame.convert_dtypes` where ``BooleanDtype`` columns were converted to ``Int64`` (:issue:`32287`)
9393
- Fixed bug in setting values using a slice indexer with string dtype (:issue:`31772`)
9494
- Fixed bug where :meth:`pandas.core.groupby.GroupBy.first` and :meth:`pandas.core.groupby.GroupBy.last` would raise a ``TypeError`` when groups contained ``pd.NA`` in a column of object dtype (:issue:`32123`)
95+
- Fixed bug where :meth:`DataFrameGroupBy.mean`, :meth:`DataFrameGroupBy.median`, :meth:`DataFrameGroupBy.var`, and :meth:`DataFrameGroupBy.std` would raise a ``TypeError`` on ``Int64`` dtype columns (:issue:`32219`)
9596

9697
**Strings**
9798

pandas/core/groupby/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ def _cython_agg_blocks(
10831083
result = type(block.values)._from_sequence(
10841084
result.ravel(), dtype=block.values.dtype
10851085
)
1086-
except ValueError:
1086+
except (ValueError, TypeError):
10871087
# reshape to be valid for non-Extension Block
10881088
result = result.reshape(1, -1)
10891089

pandas/tests/groupby/test_function.py

+31
Original file line numberDiff line numberDiff line change
@@ -1605,3 +1605,34 @@ def test_groupby_mean_no_overflow():
16051605
}
16061606
)
16071607
assert df.groupby("user")["connections"].mean()["A"] == 3689348814740003840
1608+
1609+
1610+
@pytest.mark.parametrize(
1611+
"values",
1612+
[
1613+
{
1614+
"a": [1, 1, 1, 2, 2, 2, 3, 3, 3],
1615+
"b": [1, pd.NA, 2, 1, pd.NA, 2, 1, pd.NA, 2],
1616+
},
1617+
{"a": [1, 1, 2, 2, 3, 3], "b": [1, 2, 1, 2, 1, 2]},
1618+
],
1619+
)
1620+
@pytest.mark.parametrize("function", ["mean", "median", "var"])
1621+
def test_apply_to_nullable_integer_returns_float(values, function):
1622+
# https://github.com/pandas-dev/pandas/issues/32219
1623+
output = 0.5 if function == "var" else 1.5
1624+
arr = np.array([output] * 3, dtype=float)
1625+
idx = pd.Index([1, 2, 3], dtype=object, name="a")
1626+
expected = pd.DataFrame({"b": arr}, index=idx)
1627+
1628+
groups = pd.DataFrame(values, dtype="Int64").groupby("a")
1629+
1630+
result = getattr(groups, function)()
1631+
tm.assert_frame_equal(result, expected)
1632+
1633+
result = groups.agg(function)
1634+
tm.assert_frame_equal(result, expected)
1635+
1636+
result = groups.agg([function])
1637+
expected.columns = MultiIndex.from_tuples([("b", function)])
1638+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)