From 564a066f1b0337da26abd0ee4e4b73f1835914ad Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Sat, 8 Jan 2022 15:10:50 -0500 Subject: [PATCH 1/2] Fix groupby any/all on an empty series/frame --- pandas/core/groupby/groupby.py | 4 +++- pandas/tests/groupby/test_any_all.py | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) 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..4fae388af1003 100644 --- a/pandas/tests/groupby/test_any_all.py +++ b/pandas/tests/groupby/test_any_all.py @@ -178,3 +178,14 @@ 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("box", [DataFrame, Series]) +@pytest.mark.parametrize("bool_agg_func", ["any", "all"]) +def test_empty(box, bool_agg_func): + # GH 45231 + kwargs = {"columns": ["a"]} if box is DataFrame else {"name": "a"} + obj = box(**kwargs, dtype=object) + result = getattr(obj.groupby(obj.index), bool_agg_func)() + expected = box(**kwargs, dtype=bool) + tm.assert_equal(result, expected) From 84d4e48a5d515747e86bdeeb818800976e0f91ae Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Sat, 8 Jan 2022 15:43:46 -0500 Subject: [PATCH 2/2] frame_or_series --- pandas/tests/groupby/test_any_all.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pandas/tests/groupby/test_any_all.py b/pandas/tests/groupby/test_any_all.py index 4fae388af1003..3f61a4ece66c0 100644 --- a/pandas/tests/groupby/test_any_all.py +++ b/pandas/tests/groupby/test_any_all.py @@ -180,12 +180,11 @@ def test_object_NA_raises_with_skipna_false(bool_agg_func): ser.groupby([1]).agg(bool_agg_func, skipna=False) -@pytest.mark.parametrize("box", [DataFrame, Series]) @pytest.mark.parametrize("bool_agg_func", ["any", "all"]) -def test_empty(box, bool_agg_func): +def test_empty(frame_or_series, bool_agg_func): # GH 45231 - kwargs = {"columns": ["a"]} if box is DataFrame else {"name": "a"} - obj = box(**kwargs, dtype=object) + 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 = box(**kwargs, dtype=bool) + expected = frame_or_series(**kwargs, dtype=bool) tm.assert_equal(result, expected)