Skip to content

Commit c355145

Browse files
author
Khor Chean Wei
authored
Bug Fix: pd.DataFrame.sum with min_count changes dtype if result contains NaNs (#47091)
1 parent 2e6bd77 commit c355145

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

doc/source/whatsnew/v1.5.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ Indexing
780780
- Bug in setting large integer values into :class:`Series` with ``float32`` or ``float16`` dtype incorrectly altering these values instead of coercing to ``float64`` dtype (:issue:`45844`)
781781
- Bug in :meth:`Series.asof` and :meth:`DataFrame.asof` incorrectly casting bool-dtype results to ``float64`` dtype (:issue:`16063`)
782782
- Bug in :meth:`NDFrame.xs`, :meth:`DataFrame.iterrows`, :meth:`DataFrame.loc` and :meth:`DataFrame.iloc` not always propagating metadata (:issue:`28283`)
783-
-
783+
- Bug in :meth:`DataFrame.sum` min_count changes dtype if input contains NaNs (:issue:`46947`)
784784

785785
Missing
786786
^^^^^^^

pandas/core/nanops.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1471,8 +1471,8 @@ def _maybe_null_out(
14711471
if is_numeric_dtype(result):
14721472
if np.iscomplexobj(result):
14731473
result = result.astype("c16")
1474-
else:
1475-
result = result.astype("f8")
1474+
elif not is_float_dtype(result):
1475+
result = result.astype("f8", copy=False)
14761476
result[null_mask] = np.nan
14771477
else:
14781478
# GH12941, use None to auto cast null

pandas/tests/frame/test_reductions.py

+17
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,23 @@ def test_sum_nanops_min_count(self):
778778
expected = Series([np.nan, np.nan], index=["x", "y"])
779779
tm.assert_series_equal(result, expected)
780780

781+
@pytest.mark.parametrize("float_type", ["float16", "float32", "float64"])
782+
@pytest.mark.parametrize(
783+
"kwargs, expected_result",
784+
[
785+
({"axis": 1, "min_count": 2}, [3.2, 5.3, np.NaN]),
786+
({"axis": 1, "min_count": 3}, [np.NaN, np.NaN, np.NaN]),
787+
({"axis": 1, "skipna": False}, [3.2, 5.3, np.NaN]),
788+
],
789+
)
790+
def test_sum_nanops_dtype_min_count(self, float_type, kwargs, expected_result):
791+
# GH#46947
792+
# pass
793+
df = DataFrame({"a": [1.0, 2.3, 4.4], "b": [2.2, 3, np.nan]}, dtype=float_type)
794+
result = df.sum(**kwargs)
795+
expected = Series(expected_result).astype(float_type)
796+
tm.assert_series_equal(result, expected)
797+
781798
def test_sum_object(self, float_frame):
782799
values = float_frame.values.astype(int)
783800
frame = DataFrame(values, index=float_frame.index, columns=float_frame.columns)

0 commit comments

Comments
 (0)