Skip to content

ENH: add masked algorithm for mean() function #34814

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 16 commits into from
Jan 1, 2021
Merged
Show file tree
Hide file tree
Changes from 15 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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ Deprecations
Performance improvements
~~~~~~~~~~~~~~~~~~~~~~~~
- Performance improvement in :meth:`IntervalIndex.isin` (:issue:`38353`)
-
- Performance improvement in :meth:`Series.mean` for ``ExtensionDtype`` columns (:issue:`34814`)
-

.. ---------------------------------------------------------------------------
Expand Down
9 changes: 9 additions & 0 deletions pandas/core/array_algos/masked_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,12 @@ def min(values: np.ndarray, mask: np.ndarray, *, skipna: bool = True):

def max(values: np.ndarray, mask: np.ndarray, *, skipna: bool = True):
return _minmax(np.max, values=values, mask=mask, skipna=skipna)


def mean(values: np.ndarray, mask: np.ndarray, skipna: bool = True):
if not values.size or mask.all():
return libmissing.NA
_sum = _sumprod(np.sum, values=values, mask=mask, skipna=skipna)
count = np.count_nonzero(~mask)
mean_value = _sum / count
return mean_value
2 changes: 1 addition & 1 deletion pandas/core/arrays/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ def _reduce(self, name: str, *, skipna: bool = True, **kwargs):
data = self._data
mask = self._mask

if name in {"sum", "prod", "min", "max"}:
if name in {"sum", "prod", "min", "max", "mean"}:
op = getattr(masked_reductions, name)
return op(data, mask, skipna=skipna, **kwargs)

Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/reductions/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,23 @@ def test_empty_multi(self, method, unit):
expected = Series([1, np.nan], index=["a", "b"])
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize("method", ["mean"])
@pytest.mark.parametrize("dtype", ["Float64", "Int64", "boolean"])
def test_ops_consistency_on_empty_nullable(self, method, dtype):

# GH#34814
# consistency for nullable dtypes on empty or ALL-NA mean

# empty series
eser = Series([], dtype=dtype)
result = getattr(eser, method)()
assert result is pd.NA

# ALL-NA series
nser = Series([np.nan], dtype=dtype)
result = getattr(nser, method)()
assert result is pd.NA

@pytest.mark.parametrize("method", ["mean", "median", "std", "var"])
def test_ops_consistency_on_empty(self, method):

Expand Down