Skip to content

Commit f6fb257

Browse files
authored
Fix issue #29837: added test case for aggregation with isnan (#35039)
1 parent 1684d8d commit f6fb257

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

pandas/tests/groupby/aggregate/test_aggregate.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,3 +994,30 @@ def test_groupby_get_by_index():
994994
res = df.groupby("A").agg({"B": lambda x: x.get(x.index[-1])})
995995
expected = pd.DataFrame(dict(A=["S", "W"], B=[1.0, 2.0])).set_index("A")
996996
pd.testing.assert_frame_equal(res, expected)
997+
998+
999+
def test_aggregate_categorical_with_isnan():
1000+
# GH 29837
1001+
df = pd.DataFrame(
1002+
{
1003+
"A": [1, 1, 1, 1],
1004+
"B": [1, 2, 1, 2],
1005+
"numerical_col": [0.1, 0.2, np.nan, 0.3],
1006+
"object_col": ["foo", "bar", "foo", "fee"],
1007+
"categorical_col": ["foo", "bar", "foo", "fee"],
1008+
}
1009+
)
1010+
1011+
df = df.astype({"categorical_col": "category"})
1012+
1013+
result = df.groupby(["A", "B"]).agg(lambda df: df.isna().sum())
1014+
index = pd.MultiIndex.from_arrays([[1, 1], [1, 2]], names=("A", "B"))
1015+
expected = pd.DataFrame(
1016+
data={
1017+
"numerical_col": [1.0, 0.0],
1018+
"object_col": [0, 0],
1019+
"categorical_col": [0, 0],
1020+
},
1021+
index=index,
1022+
)
1023+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)