Skip to content

BUG: Fix DataFrame reduction to preserve NaN vs <NA> in mixed dtypes (GH#62024) #62027

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

Closed
Closed
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
8 changes: 8 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -11850,6 +11850,14 @@ def _reduce(
if axis is not None:
axis = self._get_axis_number(axis)

if axis == 0:
data = self._get_numeric_data() if numeric_only else self
results = {}
for col in data.columns:
ser = data[col]
results[col] = getattr(ser, name)(skipna=skipna, **kwds)
return self._constructor_sliced(results)

def func(values: np.ndarray):
# We only use this in the case that operates on self.values
return op(values, axis=axis, skipna=skipna, **kwds)
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/frame/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,20 @@ def float_frame_with_na():
return df


def test_mixed_reduction_nan_vs_NA():
df = DataFrame(
{
"B": [1, None, 3],
"C": pd.array([1, None, 3], dtype="Int64"),
}
)
result = df.skew()
assert np.isnan(result["B"])
assert isna(result["C"]) and type(result["C"]).__name__ == "NAType"
result_B = df[["B"]].skew()
assert np.isnan(result_B["B"])


class TestDataFrameAnalytics:
# ---------------------------------------------------------------------
# Reductions
Expand Down
Loading