From d3c8faa650da33cad475d4b7567c65451aef67d7 Mon Sep 17 00:00:00 2001 From: pilkibun Date: Thu, 13 Jun 2019 21:07:03 +0300 Subject: [PATCH 1/4] TST: series.count() handling NA values for EA --- pandas/tests/extension/base/methods.py | 7 +++++++ pandas/tests/extension/decimal/test_decimal.py | 8 ++++++++ 2 files changed, 15 insertions(+) diff --git a/pandas/tests/extension/base/methods.py b/pandas/tests/extension/base/methods.py index 1852edaa9e748..c8fd4d1b708e5 100644 --- a/pandas/tests/extension/base/methods.py +++ b/pandas/tests/extension/base/methods.py @@ -30,6 +30,13 @@ def test_count(self, data_missing): expected = pd.Series([0, 1]) self.assert_series_equal(result, expected) + def test_series_count(self, data_missing): + # GH#26835 + ser = pd.Series(data_missing) + result = ser.count() + expected = 1 + assert result == expected + def test_apply_simple_series(self, data): result = pd.Series(data).apply(id) assert isinstance(result, pd.Series) diff --git a/pandas/tests/extension/decimal/test_decimal.py b/pandas/tests/extension/decimal/test_decimal.py index 97fae41bcc720..321ffcb019329 100644 --- a/pandas/tests/extension/decimal/test_decimal.py +++ b/pandas/tests/extension/decimal/test_decimal.py @@ -379,6 +379,14 @@ def test_divmod_array(reverse, expected_div, expected_mod): tm.assert_extension_array_equal(div, expected_div) tm.assert_extension_array_equal(mod, expected_mod) +def test_na_count(): + ser = pd.Series(to_decimal([1,2,3])) + n = ser.count() + assert n == 3 + for i in range(len(ser)): + ser[i] = np.nan + n -= 1 + assert ser.count() == n def test_formatting_values_deprecated(): class DecimalArray2(DecimalArray): From bb8ee3e3f63d6919f5a441d4aed81afd9d268096 Mon Sep 17 00:00:00 2001 From: pilkibun Date: Wed, 12 Jun 2019 21:51:01 +0300 Subject: [PATCH 2/4] BUG: Handle NA values for ExtensionArrays in Series.count --- pandas/core/series.py | 2 +- pandas/tests/extension/decimal/test_decimal.py | 8 -------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index f0362596920a6..9f94ecff26138 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1651,7 +1651,7 @@ def count(self, level=None): 2 """ if level is None: - return notna(com.values_from_object(self)).sum() + return notna(self.array).sum() if isinstance(level, str): level = self.index._get_level_number(level) diff --git a/pandas/tests/extension/decimal/test_decimal.py b/pandas/tests/extension/decimal/test_decimal.py index 321ffcb019329..97fae41bcc720 100644 --- a/pandas/tests/extension/decimal/test_decimal.py +++ b/pandas/tests/extension/decimal/test_decimal.py @@ -379,14 +379,6 @@ def test_divmod_array(reverse, expected_div, expected_mod): tm.assert_extension_array_equal(div, expected_div) tm.assert_extension_array_equal(mod, expected_mod) -def test_na_count(): - ser = pd.Series(to_decimal([1,2,3])) - n = ser.count() - assert n == 3 - for i in range(len(ser)): - ser[i] = np.nan - n -= 1 - assert ser.count() == n def test_formatting_values_deprecated(): class DecimalArray2(DecimalArray): From c868f437013cb9c081ad090f8a9b9dc03f4e3031 Mon Sep 17 00:00:00 2001 From: pilkibun Date: Thu, 13 Jun 2019 21:15:16 +0300 Subject: [PATCH 3/4] DOC: add whatsnew --- doc/source/whatsnew/v0.25.0.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 76ee21b4c9a50..2d14e42bee203 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -729,12 +729,14 @@ Sparse - Bug in :class:`SparseDataFrame` when adding a column in which the length of values does not match length of index, ``AssertionError`` is raised instead of raising ``ValueError`` (:issue:`25484`) - Introduce a better error message in :meth:`Series.sparse.from_coo` so it returns a ``TypeError`` for inputs that are not coo matrices (:issue:`26554`) + Other ^^^^^ - Removed unused C functions from vendored UltraJSON implementation (:issue:`26198`) - Bug in :func:`factorize` when passing an ``ExtensionArray`` with a custom ``na_sentinel`` (:issue:`25696`). - Allow :class:`Index` and :class:`RangeIndex` to be passed to numpy ``min`` and ``max`` functions (:issue:`26125`) +- :meth:`Series.count` miscounts NA values in ExtensionArrays (:issue:`26835`) .. _whatsnew_0.250.contributors: From 2bbb25616eac0d9d728aecaee062d6624e3c06a0 Mon Sep 17 00:00:00 2001 From: pilkibun Date: Tue, 18 Jun 2019 03:58:28 +0300 Subject: [PATCH 4/4] DOC: Create EA section in whatsnew --- doc/source/whatsnew/v0.25.0.rst | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.25.0.rst b/doc/source/whatsnew/v0.25.0.rst index 2d14e42bee203..d7cf8f6a24f6e 100644 --- a/doc/source/whatsnew/v0.25.0.rst +++ b/doc/source/whatsnew/v0.25.0.rst @@ -729,14 +729,17 @@ Sparse - Bug in :class:`SparseDataFrame` when adding a column in which the length of values does not match length of index, ``AssertionError`` is raised instead of raising ``ValueError`` (:issue:`25484`) - Introduce a better error message in :meth:`Series.sparse.from_coo` so it returns a ``TypeError`` for inputs that are not coo matrices (:issue:`26554`) +ExtensionArray +^^^^^^^^^^^^^^ + +- Bug in :func:`factorize` when passing an ``ExtensionArray`` with a custom ``na_sentinel`` (:issue:`25696`). +- :meth:`Series.count` miscounts NA values in ExtensionArrays (:issue:`26835`) Other ^^^^^ - Removed unused C functions from vendored UltraJSON implementation (:issue:`26198`) -- Bug in :func:`factorize` when passing an ``ExtensionArray`` with a custom ``na_sentinel`` (:issue:`25696`). - Allow :class:`Index` and :class:`RangeIndex` to be passed to numpy ``min`` and ``max`` functions (:issue:`26125`) -- :meth:`Series.count` miscounts NA values in ExtensionArrays (:issue:`26835`) .. _whatsnew_0.250.contributors: