Skip to content

Commit b38357b

Browse files
authored
Backport PR pandas-dev#32223: BUG: Fix DateFrameGroupBy.mean error for Int64 dtype (pandas-dev#32649)
1 parent 5e52303 commit b38357b

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
@@ -94,6 +94,7 @@ Bug fixes
9494
- Fixed bug in setting values using a slice indexer with string dtype (:issue:`31772`)
9595
- 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`)
9696
- Fix bug in :meth:`Series.convert_dtypes` for series with mix of integers and strings (:issue:`32117`)
97+
- 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`)
9798

9899
**Strings**
99100

pandas/core/groupby/generic.py

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

pandas/tests/groupby/test_function.py

+31
Original file line numberDiff line numberDiff line change
@@ -1563,3 +1563,34 @@ def test_groupby_mean_no_overflow():
15631563
}
15641564
)
15651565
assert df.groupby("user")["connections"].mean()["A"] == 3689348814740003840
1566+
1567+
1568+
@pytest.mark.parametrize(
1569+
"values",
1570+
[
1571+
{
1572+
"a": [1, 1, 1, 2, 2, 2, 3, 3, 3],
1573+
"b": [1, pd.NA, 2, 1, pd.NA, 2, 1, pd.NA, 2],
1574+
},
1575+
{"a": [1, 1, 2, 2, 3, 3], "b": [1, 2, 1, 2, 1, 2]},
1576+
],
1577+
)
1578+
@pytest.mark.parametrize("function", ["mean", "median", "var"])
1579+
def test_apply_to_nullable_integer_returns_float(values, function):
1580+
# https://github.com/pandas-dev/pandas/issues/32219
1581+
output = 0.5 if function == "var" else 1.5
1582+
arr = np.array([output] * 3, dtype=float)
1583+
idx = pd.Index([1, 2, 3], dtype=object, name="a")
1584+
expected = pd.DataFrame({"b": arr}, index=idx)
1585+
1586+
groups = pd.DataFrame(values, dtype="Int64").groupby("a")
1587+
1588+
result = getattr(groups, function)()
1589+
tm.assert_frame_equal(result, expected)
1590+
1591+
result = groups.agg(function)
1592+
tm.assert_frame_equal(result, expected)
1593+
1594+
result = groups.agg([function])
1595+
expected.columns = MultiIndex.from_tuples([("b", function)])
1596+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)