From e14c5505ff25f245bfdd6f8bc901279dabe34a38 Mon Sep 17 00:00:00 2001 From: topper-123 Date: Sun, 28 Oct 2018 03:26:44 +0000 Subject: [PATCH] add np.nan* funcs to cython_table (#22109) --- doc/source/whatsnew/v0.24.0.txt | 1 + pandas/core/base.py | 12 ++++++++++++ pandas/tests/groupby/aggregate/test_other.py | 11 ----------- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/doc/source/whatsnew/v0.24.0.txt b/doc/source/whatsnew/v0.24.0.txt index b8d63e03e3318..51c398518c153 100644 --- a/doc/source/whatsnew/v0.24.0.txt +++ b/doc/source/whatsnew/v0.24.0.txt @@ -1086,6 +1086,7 @@ Numeric - Bug in :meth:`DataFrame.astype` to extension dtype may raise ``AttributeError`` (:issue:`22578`) - Bug in :class:`DataFrame` with ``timedelta64[ns]`` dtype arithmetic operations with ``ndarray`` with integer dtype incorrectly treating the narray as ``timedelta64[ns]`` dtype (:issue:`23114`) - Bug in :meth:`Series.rpow` with object dtype ``NaN`` for ``1 ** NA`` instead of ``1`` (:issue:`22922`). +- :meth:`Series.agg` can now handle numpy NaN-aware methods like :func:`numpy.nansum` (:issue:`19629`) Strings ^^^^^^^ diff --git a/pandas/core/base.py b/pandas/core/base.py index 88a36b0ecc7c7..de368f52b6f00 100644 --- a/pandas/core/base.py +++ b/pandas/core/base.py @@ -178,11 +178,13 @@ class SelectionMixin(object): _selection = None _internal_names = ['_cache', '__setstate__'] _internal_names_set = set(_internal_names) + _builtin_table = OrderedDict(( (builtins.sum, np.sum), (builtins.max, np.max), (builtins.min, np.min), )) + _cython_table = OrderedDict(( (builtins.sum, 'sum'), (builtins.max, 'max'), @@ -190,15 +192,25 @@ class SelectionMixin(object): (np.all, 'all'), (np.any, 'any'), (np.sum, 'sum'), + (np.nansum, 'sum'), (np.mean, 'mean'), + (np.nanmean, 'mean'), (np.prod, 'prod'), + (np.nanprod, 'prod'), (np.std, 'std'), + (np.nanstd, 'std'), (np.var, 'var'), + (np.nanvar, 'var'), (np.median, 'median'), + (np.nanmedian, 'median'), (np.max, 'max'), + (np.nanmax, 'max'), (np.min, 'min'), + (np.nanmin, 'min'), (np.cumprod, 'cumprod'), + (np.nancumprod, 'cumprod'), (np.cumsum, 'cumsum'), + (np.nancumsum, 'cumsum'), )) @property diff --git a/pandas/tests/groupby/aggregate/test_other.py b/pandas/tests/groupby/aggregate/test_other.py index 61db4cee1ab02..c35405ad739c9 100644 --- a/pandas/tests/groupby/aggregate/test_other.py +++ b/pandas/tests/groupby/aggregate/test_other.py @@ -487,17 +487,6 @@ def test_agg_structs_series(structure, expected): tm.assert_series_equal(result, expected) -@pytest.mark.parametrize('observed', [ - True, - pytest.param(False, - marks=pytest.mark.xfail(reason="GH#18869: agg func not " - "called on empty groups.", - strict=True)), - pytest.param(None, - marks=pytest.mark.xfail(reason="GH#18869: agg func not " - "called on empty groups.", - strict=True)) -]) def test_agg_category_nansum(observed): categories = ['a', 'b', 'c'] df = pd.DataFrame({"A": pd.Categorical(['a', 'a', 'b'],