Skip to content

[backport 2.3.x] BUG(string dtype): Empty sum produces incorrect result (#60936) #61625

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
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
9 changes: 8 additions & 1 deletion pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2386,7 +2386,14 @@ def _groupby_op(
if op.how not in ["any", "all"]:
# Fail early to avoid conversion to object
op._get_cython_function(op.kind, op.how, np.dtype(object), False)
npvalues = self.to_numpy(object, na_value=np.nan)

arr = self
if op.how == "sum":
# https://github.com/pandas-dev/pandas/issues/60229
# All NA should result in the empty string.
if min_count == 0:
arr = arr.fillna("")
npvalues = arr.to_numpy(object, na_value=np.nan)
else:
raise NotImplementedError(
f"function is not implemented for this dtype: {self.dtype}"
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/frame/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,16 @@ def test_axis_1_empty(self, all_reductions, index):
expected = Series([], index=index, dtype=expected_dtype)
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize("min_count", [0, 1])
def test_axis_1_sum_na(self, string_dtype_no_object, skipna, min_count):
# https://github.com/pandas-dev/pandas/issues/60229
dtype = string_dtype_no_object
df = DataFrame({"a": [pd.NA]}, dtype=dtype)
result = df.sum(axis=1, skipna=skipna, min_count=min_count)
value = "" if skipna and min_count == 0 else pd.NA
expected = Series([value], dtype=dtype)
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize("method, unit", [("sum", 0), ("prod", 1)])
@pytest.mark.parametrize("numeric_only", [None, True, False])
def test_sum_prod_nanops(self, method, unit, numeric_only):
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/groupby/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,20 @@ def test_min_empty_string_dtype(func, string_dtype_no_object):
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize("min_count", [0, 1])
def test_string_dtype_empty_sum(string_dtype_no_object, min_count):
# https://github.com/pandas-dev/pandas/issues/60229
dtype = string_dtype_no_object
df = DataFrame({"a": ["x"], "b": [pd.NA]}, dtype=dtype)
gb = df.groupby("a")
result = gb.sum(min_count=min_count)
value = "" if min_count == 0 else pd.NA
expected = DataFrame(
{"b": value}, index=pd.Index(["x"], name="a", dtype=dtype), dtype=dtype
)
tm.assert_frame_equal(result, expected)


def test_max_nan_bug():
df = DataFrame(
{
Expand Down
25 changes: 25 additions & 0 deletions pandas/tests/resample/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,31 @@ def test_resample_empty_series(freq, empty_series_dti, resample_method):
assert result.index.freq == expected.index.freq


@pytest.mark.parametrize("min_count", [0, 1])
def test_resample_empty_sum_string(string_dtype_no_object, min_count):
# https://github.com/pandas-dev/pandas/issues/60229
dtype = string_dtype_no_object
ser = Series(
pd.NA,
index=DatetimeIndex(
[
"2000-01-01 00:00:00",
"2000-01-01 00:00:10",
"2000-01-01 00:00:20",
"2000-01-01 00:00:30",
]
),
dtype=dtype,
)
rs = ser.resample("20s")
result = rs.sum(min_count=min_count)

value = "" if min_count == 0 else pd.NA
index = date_range(start="2000-01-01", freq="20s", periods=2)
expected = Series(value, index=index, dtype=dtype)
tm.assert_series_equal(result, expected)


@all_ts
@pytest.mark.parametrize(
"freq",
Expand Down
20 changes: 20 additions & 0 deletions pandas/tests/resample/test_resampler_grouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,26 @@ def test_resample_groupby_agg_object_dtype_all_nan(consolidate):
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize("min_count", [0, 1])
def test_groupby_resample_empty_sum_string(
string_dtype_no_object, test_frame, min_count
):
# https://github.com/pandas-dev/pandas/issues/60229
dtype = string_dtype_no_object
test_frame = test_frame.assign(B=pd.array([pd.NA] * len(test_frame), dtype=dtype))
gbrs = test_frame.groupby("A").resample("40s", include_groups=False)
result = gbrs.sum(min_count=min_count)

index = pd.MultiIndex(
levels=[[1, 2, 3], [pd.to_datetime("2000-01-01", unit="ns")]],
codes=[[0, 1, 2], [0, 0, 0]],
names=["A", None],
)
value = "" if min_count == 0 else pd.NA
expected = DataFrame({"B": value}, index=index, dtype=dtype)
tm.assert_frame_equal(result, expected)


def test_groupby_resample_with_list_of_keys():
# GH 47362
df = DataFrame(
Expand Down
Loading