Skip to content

BUG: fix fill value for gouped sum in case of unobserved categories for string dtype (empty string instead of 0) #61909

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions pandas/_libs/groupby.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def group_sum(
result_mask: np.ndarray | None = ...,
min_count: int = ...,
is_datetimelike: bool = ...,
initial: object = ...,
skipna: bool = ...,
) -> None: ...
def group_prod(
Expand Down
13 changes: 10 additions & 3 deletions pandas/_libs/groupby.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,7 @@ def group_sum(
uint8_t[:, ::1] result_mask=None,
Py_ssize_t min_count=0,
bint is_datetimelike=False,
object initial=0,
bint skipna=True,
) -> None:
"""
Expand All @@ -725,9 +726,15 @@ def group_sum(
raise ValueError("len(index) != len(labels)")

nobs = np.zeros((<object>out).shape, dtype=np.int64)
# the below is equivalent to `np.zeros_like(out)` but faster
sumx = np.zeros((<object>out).shape, dtype=(<object>out).base.dtype)
compensation = np.zeros((<object>out).shape, dtype=(<object>out).base.dtype)
if initial == 0:
# the below is equivalent to `np.zeros_like(out)` but faster
sumx = np.zeros((<object>out).shape, dtype=(<object>out).base.dtype)
compensation = np.zeros((<object>out).shape, dtype=(<object>out).base.dtype)
else:
# in practice this path is only taken for strings to use empty string as initial
assert sum_t is object
sumx = np.full((<object>out).shape, initial, dtype=object)
# object code path does not use `compensation`

N, K = (<object>values).shape
if uses_mask:
Expand Down
3 changes: 3 additions & 0 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2608,6 +2608,7 @@ def _groupby_op(
kind = WrappedCythonOp.get_kind_from_how(how)
op = WrappedCythonOp(how=how, kind=kind, has_dropped_na=has_dropped_na)

initial: Any = 0
# GH#43682
if isinstance(self.dtype, StringDtype):
# StringArray
Expand All @@ -2632,6 +2633,7 @@ def _groupby_op(

arr = self
if op.how == "sum":
initial = ""
# https://github.com/pandas-dev/pandas/issues/60229
# All NA should result in the empty string.
assert "skipna" in kwargs
Expand All @@ -2649,6 +2651,7 @@ def _groupby_op(
ngroups=ngroups,
comp_ids=ids,
mask=None,
initial=initial,
**kwargs,
)

Expand Down
9 changes: 9 additions & 0 deletions pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import functools
from typing import (
TYPE_CHECKING,
Any,
Generic,
final,
)
Expand Down Expand Up @@ -319,6 +320,7 @@ def _cython_op_ndim_compat(
comp_ids: np.ndarray,
mask: npt.NDArray[np.bool_] | None = None,
result_mask: npt.NDArray[np.bool_] | None = None,
initial: Any = 0,
**kwargs,
) -> np.ndarray:
if values.ndim == 1:
Expand All @@ -335,6 +337,7 @@ def _cython_op_ndim_compat(
comp_ids=comp_ids,
mask=mask,
result_mask=result_mask,
initial=initial,
**kwargs,
)
if res.shape[0] == 1:
Expand All @@ -350,6 +353,7 @@ def _cython_op_ndim_compat(
comp_ids=comp_ids,
mask=mask,
result_mask=result_mask,
initial=initial,
**kwargs,
)

Expand All @@ -363,6 +367,7 @@ def _call_cython_op(
comp_ids: np.ndarray,
mask: npt.NDArray[np.bool_] | None,
result_mask: npt.NDArray[np.bool_] | None,
initial: Any = 0,
**kwargs,
) -> np.ndarray: # np.ndarray[ndim=2]
orig_values = values
Expand Down Expand Up @@ -420,6 +425,10 @@ def _call_cython_op(
"sum",
"median",
]:
if self.how == "sum":
# pass in through kwargs only for sum (other functions don't have
# the keyword)
kwargs["initial"] = initial
func(
out=result,
counts=counts,
Expand Down
19 changes: 13 additions & 6 deletions pandas/tests/groupby/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ def f(a):
return a

index = MultiIndex.from_product(map(f, args), names=names)
if isinstance(fill_value, dict):
# fill_value is a dict mapping column names to fill values
# -> reindex column by column (reindex itself does not support this)
res = {}
for col in result.columns:
res[col] = result[col].reindex(index, fill_value=fill_value[col])
return DataFrame(res, index=index).sort_index()

return result.reindex(index, fill_value=fill_value).sort_index()


Expand Down Expand Up @@ -317,18 +325,14 @@ def test_apply(ordered):
tm.assert_series_equal(result, expected)


def test_observed(request, using_infer_string, observed):
def test_observed(observed, using_infer_string):
# multiple groupers, don't re-expand the output space
# of the grouper
# gh-14942 (implement)
# gh-10132 (back-compat)
# gh-8138 (back-compat)
# gh-8869

if using_infer_string and not observed:
# TODO(infer_string) this fails with filling the string column with 0
request.applymarker(pytest.mark.xfail(reason="TODO(infer_string)"))

cat1 = Categorical(["a", "a", "b", "b"], categories=["a", "b", "z"], ordered=True)
cat2 = Categorical(["c", "d", "c", "d"], categories=["c", "d", "y"], ordered=True)
df = DataFrame({"A": cat1, "B": cat2, "values": [1, 2, 3, 4]})
Expand Down Expand Up @@ -356,7 +360,10 @@ def test_observed(request, using_infer_string, observed):
result = gb.sum()
if not observed:
expected = cartesian_product_for_groupers(
expected, [cat1, cat2], list("AB"), fill_value=0
expected,
[cat1, cat2],
list("AB"),
fill_value={"values": 0, "C": ""} if using_infer_string else 0,
)

tm.assert_frame_equal(result, expected)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/groupby/test_timegrouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def test_groupby_with_timegrouper(self, using_infer_string):
unit=df.index.unit,
)
expected = DataFrame(
{"Buyer": 0, "Quantity": 0},
{"Buyer": "" if using_infer_string else 0, "Quantity": 0},
index=exp_dti,
)
# Cast to object/str to avoid implicit cast when setting
Expand Down
Loading