diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 9ceb50611f1c5..e4c5541468629 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -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) diff --git a/pandas/tests/groupby/test_any_all.py b/pandas/tests/groupby/test_any_all.py index 13232d454a48c..3f61a4ece66c0 100644 --- a/pandas/tests/groupby/test_any_all.py +++ b/pandas/tests/groupby/test_any_all.py @@ -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)