-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Bug Fix: pd.DataFrame.sum with min_count changes dtype if result contains NaNs #47091
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
Changes from 10 commits
73da55e
816f64c
23c19b8
8177bf9
b1828fe
b5590c4
181dea4
273f17a
832ef59
0d3254d
070a963
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1472,7 +1472,8 @@ def _maybe_null_out( | |
if np.iscomplexobj(result): | ||
result = result.astype("c16") | ||
else: | ||
result = result.astype("f8") | ||
if not is_float_dtype(result): | ||
result = result.astype("f8") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please make this an elif There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add |
||
result[null_mask] = np.nan | ||
else: | ||
# GH12941, use None to auto cast null | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -778,6 +778,20 @@ def test_sum_nanops_min_count(self): | |
expected = Series([np.nan, np.nan], index=["x", "y"]) | ||
tm.assert_series_equal(result, expected) | ||
|
||
@pytest.mark.parametrize( | ||
"kwargs", | ||
[ | ||
{"axis": 1, "min_count": 1}, | ||
{"axis": 1, "min_count": 2}, | ||
{"axis": 1, "skipna": False}, | ||
], | ||
) | ||
def test_sum_nanops_dtype_min_count(self, kwargs): | ||
# GH#46947 | ||
df = DataFrame({"a": [1.0, 2.3, 4.4], "b": [2.2, 3, np.nan]}, dtype="float32") | ||
result = df.sum(**kwargs).dtype | ||
assert result == "float32" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please test the whole DataFrame and parametrize over all possible float dtypes. |
||
|
||
def test_sum_object(self, float_frame): | ||
values = float_frame.values.astype(int) | ||
frame = DataFrame(values, index=float_frame.index, columns=float_frame.columns) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_maybe_null_out
is private. Please reference the public functions, e.g. sumThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok Have updated