Skip to content

Commit 4f6ee28

Browse files
committed
made tests and changes for issue 58031 on GH
1 parent c468028 commit 4f6ee28

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

pandas/core/groupby/ops.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,9 @@ def agg_series(
914914
np.ndarray or ExtensionArray
915915
"""
916916

917-
if not isinstance(obj._values, np.ndarray):
917+
918+
#if objtype is not in np.dtypes, type is preserved but thats bad seems readable
919+
if not isinstance(obj._values, np.ndarray) and obj.dtype != "boolean":
918920
# we can preserve a little bit more aggressively with EA dtype
919921
# because maybe_cast_pointwise_result will do a try/except
920922
# with _from_sequence. NB we are assuming here that _from_sequence

pandas/tests/groupby/aggregate/test_other.py

+17
Original file line numberDiff line numberDiff line change
@@ -666,3 +666,20 @@ def weird_func(x):
666666

667667
result = df["decimals"].groupby(df["id1"]).agg(weird_func)
668668
tm.assert_series_equal(result, expected, check_names=False)
669+
670+
def test_groupby_agg_boolean_dype():
671+
# GH Issue #58031
672+
# Ensure return type of aggregate dtype has consistent behavior for 'bool' and 'boolean'
673+
# because boolean not covered under numpy
674+
675+
df_boolean = pd.DataFrame({0: [1, 2, 2], 1: [True, True, None]})
676+
df_boolean[1] = df_boolean[1].astype("boolean")
677+
678+
df_bool = pd.DataFrame({0: [1, 2, 2], 1: [True, True, None]})
679+
df_bool[1] = df_bool[1].astype("bool")
680+
681+
boolean_return_type = df_boolean.groupby(by=0).aggregate(lambda s: s.fillna(False).mean()).dtypes.values[0]
682+
bool_return_type = df_bool.groupby(by=0).aggregate(lambda s: s.fillna(False).mean()).dtypes.values[0]
683+
684+
assert boolean_return_type == bool_return_type
685+

0 commit comments

Comments
 (0)