Skip to content

Commit

Permalink
Fix groupby any/all on an empty series/frame (pandas-dev#45274)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhshadrach authored Jan 10, 2022
1 parent 2cb60c7 commit e5cf25b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1750,7 +1750,9 @@ def objs_to_bool(vals: ArrayLike) -> tuple[np.ndarray, type]:
if is_object_dtype(vals.dtype):
# GH#37501: don't raise on pd.NA when skipna=True
if skipna:
func = np.vectorize(lambda x: bool(x) if not isna(x) else True)
func = np.vectorize(
lambda x: bool(x) if not isna(x) else True, otypes=[bool]
)
vals = func(vals)
else:
vals = vals.astype(bool, copy=False)
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/groupby/test_any_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,13 @@ def test_object_NA_raises_with_skipna_false(bool_agg_func):
ser = Series([pd.NA], dtype=object)
with pytest.raises(TypeError, match="boolean value of NA is ambiguous"):
ser.groupby([1]).agg(bool_agg_func, skipna=False)


@pytest.mark.parametrize("bool_agg_func", ["any", "all"])
def test_empty(frame_or_series, bool_agg_func):
# GH 45231
kwargs = {"columns": ["a"]} if frame_or_series is DataFrame else {"name": "a"}
obj = frame_or_series(**kwargs, dtype=object)
result = getattr(obj.groupby(obj.index), bool_agg_func)()
expected = frame_or_series(**kwargs, dtype=bool)
tm.assert_equal(result, expected)

0 comments on commit e5cf25b

Please sign in to comment.