Skip to content

Commit 782dc18

Browse files
REGR: reduction operations failing if min_count is larger (#40143)
1 parent 529adea commit 782dc18

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

doc/source/whatsnew/v1.2.4.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ including other versions of pandas.
1515
Fixed regressions
1616
~~~~~~~~~~~~~~~~~
1717

18-
-
18+
- Fixed regression in :meth:`DataFrame.sum` when ``min_count`` greater than the :class:`DataFrame` shape was passed resulted in a ``ValueError`` (:issue:`39738`)
1919
-
2020

2121
.. ---------------------------------------------------------------------------

pandas/core/frame.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -9175,6 +9175,7 @@ def _reduce(
91759175
**kwds,
91769176
):
91779177

9178+
min_count = kwds.get("min_count", 0)
91789179
assert filter_type is None or filter_type == "bool", filter_type
91799180
out_dtype = "bool" if filter_type == "bool" else None
91809181

@@ -9219,7 +9220,7 @@ def _get_data() -> DataFrame:
92199220
data = self._get_bool_data()
92209221
return data
92219222

9222-
if numeric_only is not None or axis == 0:
9223+
if (numeric_only is not None or axis == 0) and min_count == 0:
92239224
# For numeric_only non-None and axis non-None, we know
92249225
# which blocks to use and no try/except is needed.
92259226
# For numeric_only=None only the case with axis==0 and no object
@@ -9236,7 +9237,7 @@ def _get_data() -> DataFrame:
92369237

92379238
# After possibly _get_data and transposing, we are now in the
92389239
# simple case where we can use BlockManager.reduce
9239-
res, indexer = df._mgr.reduce(blk_func, ignore_failures=ignore_failures)
9240+
res, _ = df._mgr.reduce(blk_func, ignore_failures=ignore_failures)
92409241
out = df._constructor(res).iloc[0]
92419242
if out_dtype is not None:
92429243
out = out.astype(out_dtype)
@@ -9264,14 +9265,15 @@ def _get_data() -> DataFrame:
92649265
with np.errstate(all="ignore"):
92659266
result = func(values)
92669267

9267-
if filter_type == "bool" and notna(result).all():
9268-
result = result.astype(np.bool_)
9269-
elif filter_type is None and is_object_dtype(result.dtype):
9270-
try:
9271-
result = result.astype(np.float64)
9272-
except (ValueError, TypeError):
9273-
# try to coerce to the original dtypes item by item if we can
9274-
pass
9268+
if hasattr(result, "dtype"):
9269+
if filter_type == "bool" and notna(result).all():
9270+
result = result.astype(np.bool_)
9271+
elif filter_type is None and is_object_dtype(result.dtype):
9272+
try:
9273+
result = result.astype(np.float64)
9274+
except (ValueError, TypeError):
9275+
# try to coerce to the original dtypes item by item if we can
9276+
pass
92759277

92769278
result = self._constructor_sliced(result, index=labels)
92779279
return result

pandas/core/nanops.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import functools
24
import itertools
35
import operator
@@ -1380,7 +1382,7 @@ def _maybe_null_out(
13801382
mask: Optional[np.ndarray],
13811383
shape: Tuple[int, ...],
13821384
min_count: int = 1,
1383-
) -> float:
1385+
) -> np.ndarray | float:
13841386
"""
13851387
Returns
13861388
-------

pandas/tests/frame/test_reductions.py

+7
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,13 @@ def test_sum_nanops_timedelta(self):
847847
expected = Series([0, 0, np.nan], dtype="m8[ns]", index=idx)
848848
tm.assert_series_equal(result, expected)
849849

850+
def test_sum_nanops_min_count(self):
851+
# https://github.com/pandas-dev/pandas/issues/39738
852+
df = DataFrame({"x": [1, 2, 3], "y": [4, 5, 6]})
853+
result = df.sum(min_count=10)
854+
expected = Series([np.nan, np.nan], index=["x", "y"])
855+
tm.assert_series_equal(result, expected)
856+
850857
def test_sum_object(self, float_frame):
851858
values = float_frame.values.astype(int)
852859
frame = DataFrame(values, index=float_frame.index, columns=float_frame.columns)

0 commit comments

Comments
 (0)