8000 [BUG] Sum of grouped bool has inconsistent dtype by rhshadrach · Pull Request #32894 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

[BUG] Sum of grouped bool has inconsistent dtype #32894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 26, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[BUG] Aggregated bool has inconsistent dtype
Addresses: GH7001
  • Loading branch information
rhshadrach committed Mar 25, 2020
commit 465a69b8350a6af53fb0ae5ace981bbf09f77db7
8 changes: 7 additions & 1 deletion pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
ensure_int64,
ensure_object,
ensure_str,
groupby_result_dtype,
is_bool,
is_bool_dtype,
is_complex,
Expand Down Expand Up @@ -172,7 +173,9 @@ def maybe_downcast_to_dtype(result, dtype):
return result


def maybe_downcast_numeric(result, dtype, do_round: bool = False):
def maybe_downcast_numeric(
result, dtype, do_round: bool = False, how: str = "",
):
"""
Subset of maybe_downcast_to_dtype restricted to numeric dtypes.

Expand All @@ -181,6 +184,7 @@ def maybe_downcast_numeric(result, dtype, do_round: bool = False):
result : ndarray or ExtensionArray
dtype : np.dtype or ExtensionDtype
do_round : bool
how : str

Returns
-------
Expand All @@ -195,6 +199,8 @@ def maybe_downcast_numeric(result, dtype, do_round: bool = False):
# earlier
result = np.array(result)

dtype = groupby_result_dtype(dtype, how)

def trans(x):
if do_round:
return x.round()
Expand Down
24 changes: 24 additions & 0 deletions pandas/core/dtypes/common.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -1788,3 +1788,27 @@ def pandas_dtype(dtype) -> DtypeObj:
raise TypeError(f"dtype '{dtype}' not understood")

return npdtype


def groupby_result_dtype(dtype, how) -> DtypeObj:
"""
Get the desired dtype of an aggregation result based on the
input dtype and how the aggregation is done.

Parameters
----------
dtype : dtype, type
The input dtype for the groupby.
how : str
How the aggregation is performed.

Returns
-------
The desired dtype of the aggregation result.
"""
d = {
(np.dtype(np.bool), "add"): np.dtype(np.int64),
(np.dtype(np.bool), "cumsum"): np.dtype(np.int64),
(np.dtype(np.bool), "sum"): np.dtype(np.int64),
}
return d.get((dtype, how), dtype)
6 changes: 3 additions & 3 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ def _transform_fast(self, result, func_nm: str) -> Series:
cast = self._transform_should_cast(func_nm)
out = algorithms.take_1d(result._values, ids)
if cast:
out = self._try_cast(out, self.obj)
out = self._try_cast(out, self.obj, how=func_nm)
return Series(out, index=self.obj.index, name=self.obj.name)

def filter(self, func, dropna=True, *args, **kwargs):
Expand Down Expand Up @@ -1073,7 +1073,7 @@ def _cython_agg_blocks(

if result is not no_result:
# see if we can cast the block back to the original dtype
result = maybe_downcast_numeric(result, block.dtype)
result = maybe_downcast_numeric(result, block.dtype, how=how)

if block.is_extension and isinstance(result, np.ndarray):
# e.g. block.values was an IntegerArray
Expand Down Expand Up @@ -1460,7 +1460,7 @@ def _transform_fast(self, result: DataFrame, func_nm: str) -> DataFrame:
# TODO: we have no test cases that get here with EA dtypes;
# try_cast may not be needed if EAs never get here
if cast:
res = self._try_cast(res, obj.iloc[:, i])
res = self._try_cast(res, obj.iloc[:, i], how=func_nm)
output.append(res)

return DataFrame._from_arrays(output, columns=result.columns, index=obj.index)
Expand Down
11 changes: 7 additions & 4 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class providing the base-class of operations.
from pandas.core.dtypes.cast import maybe_downcast_to_dtype
from pandas.core.dtypes.common import (
ensure_float,
groupby_result_dtype,
is_datetime64_dtype,
is_extension_array_dtype,
is_integer_dtype,
Expand Down Expand Up @@ -792,7 +793,7 @@ def _cumcount_array(self, ascending: bool = True):
rev[sorter] = np.arange(count, dtype=np.intp)
return out[rev].astype(np.int64, copy=False)

def _try_cast(self, result, obj, numeric_only: bool = False):
def _try_cast(self, result, obj, numeric_only: bool = False, how: str = ""):
"""
Try to cast the result to our obj original type,
we may have roundtripped through object in the mean-time.
Expand All @@ -806,6 +807,8 @@ def _try_cast(self, result, obj, numeric_only: bool = False):
else:
dtype = obj.dtype

dtype = groupby_result_dtype(dtype, how)

if not is_scalar(result):
if is_extension_array_dtype(dtype) and dtype.kind != "M":
# The function can return something of any type, so check
Expand Down Expand Up @@ -852,7 +855,7 @@ def _cython_transform(self, how: str, numeric_only: bool = True, **kwargs):
continue

if self._transform_should_cast(how):
result = self._ 67ED try_cast(result, obj)
result = self._try_cast(result, obj, how=how)

key = base.OutputKey(label=name, position=idx)
output[key] = result
Expand Down Expand Up @@ -895,12 +898,12 @@ def _cython_agg_general(
assert len(agg_names) == result.shape[1]
for result_column, result_name in zip(result.T, agg_names):
key = base.OutputKey(label=result_name, position=idx)
output[key] = self._try_cast(result_column, obj)
output[key] = self._try_cast(result_column, obj, how=how)
idx += 1
else:
assert result.ndim == 1
key = base.OutputKey(label=name, position=idx)
output[key] = self._try_cast(result, obj)
output[key] = self._try_cast(result, obj, how=how)
idx += 1

if len(output) == 0:
Expand Down
45 changes: 45 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

from pandas.errors import PerformanceWarning

from pandas.core.dtypes.common import is_integer_dtype

import pandas as pd
from pandas import DataFrame, Index, MultiIndex, Series, Timestamp, date_range, read_csv
import pandas._testing as tm
Expand Down Expand Up @@ -2057,3 +2059,46 @@ def test_groups_repr_truncates(max_seq_items, expected):

result = df.groupby(np.array(df.a)).groups.__repr__()
assert result == expected


def test_bool_agg_dtype():
# GH 7001
# Bool aggregation results in int
df = pd.DataFrame({"a": [1, 1], "b": [False, True]})
s = df.set_index("a")["b"]

result = df.groupby("a").sum()["b"].dtype
assert is_integer_dtype(result)

result = s.groupby("a").sum().dtype
assert is_integer_dtype(result)

result = df.groupby("a").cumsum()["b"].dtype
assert is_integer_dtype(result)

result = s.groupby("a").cumsum().dtype
assert is_integer_dtype(result)

result = df.groupby("a").agg("sum")["b"].dtype
assert is_integer_dtype(result)

result = s.groupby("a").agg("sum").dtype
assert is_integer_dtype(result)

result = df.groupby("a").agg("cumsum")["b"].dtype
assert is_integer_dtype(result)

result = s.groupby("a").agg("cumsum").dtype
assert is_integer_dtype(result)

result = df.groupby("a").transform("sum")["b"].dtype
assert is_integer_dtype(result)

result = s.groupby("a").transform("sum").dtype
assert is_integer_dtype(result)

result = df.groupby("a").transform("cumsum")["b"].dtype
assert is_integer_dtype(result)

result = s.groupby("a").transform("cumsum").dtype
assert is_integer_dtype(result)
0