From 898583124beb74cb59244e23efaa21988adba40e Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Fri, 17 Apr 2020 21:53:38 -0500 Subject: [PATCH 01/15] BUG: Fix use_inf_as_na bugs --- doc/source/whatsnew/v1.1.0.rst | 3 ++- pandas/_libs/missing.pyx | 7 ++++- pandas/core/dtypes/missing.py | 6 ++++- .../tests/arrays/categorical/test_missing.py | 26 ++++++++++++++++++- pandas/tests/arrays/string_/test_string.py | 22 ++++++++++++++++ 5 files changed, 60 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index a797090a83444..e9696a11fc148 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -443,6 +443,7 @@ Categorical - :meth:`Categorical.fillna` now accepts :class:`Categorical` ``other`` argument (:issue:`32420`) - Bug where :meth:`Categorical.replace` would replace with ``NaN`` whenever the new value and replacement value were equal (:issue:`33288`) - Bug where an ordered :class:`Categorical` containing only ``NaN`` values would raise rather than returning ``NaN`` when taking the minimum or maximum (:issue:`33450`) +- Bug where :meth:`Series.isna` and :meth:`DataFrame.isna` would raise for categorical dtype when ``pandas.options.mode.use_inf_as_na`` was set to ``True`` (:issue:`33594`) Datetimelike ^^^^^^^^^^^^ @@ -636,7 +637,7 @@ ExtensionArray ^^^^^^^^^^^^^^ - Fixed bug where :meth:`Serires.value_counts` would raise on empty input of ``Int64`` dtype (:issue:`33317`) -- +- Fixed bug where :class:`StringArray` would not return ``True`` for NA values when ``pandas.options.mode.use_inf_as_na`` was set to ``True`` Other diff --git a/pandas/_libs/missing.pyx b/pandas/_libs/missing.pyx index dacf454824190..b2c95b507eaee 100644 --- a/pandas/_libs/missing.pyx +++ b/pandas/_libs/missing.pyx @@ -140,6 +140,7 @@ def isnaobj_old(arr: ndarray) -> ndarray: - INF - NEGINF - NaT + - NA Parameters ---------- @@ -160,7 +161,11 @@ def isnaobj_old(arr: ndarray) -> ndarray: result = np.zeros(n, dtype=np.uint8) for i in range(n): val = arr[i] - result[i] = val is NaT or _check_none_nan_inf_neginf(val) + result[i] = ( + val is NaT + or _check_none_nan_inf_neginf(val) + or val is C_NA + ) return result.view(np.bool_) diff --git a/pandas/core/dtypes/missing.py b/pandas/core/dtypes/missing.py index 08a6d42042c1c..949842afac6a0 100644 --- a/pandas/core/dtypes/missing.py +++ b/pandas/core/dtypes/missing.py @@ -231,7 +231,11 @@ def _isna_ndarraylike(obj): def _isna_ndarraylike_old(obj): - values = getattr(obj, "_values", obj) + if not isinstance(obj, np.ndarray): + values = obj.to_numpy() + else: + values = obj + dtype = values.dtype if is_string_dtype(dtype): diff --git a/pandas/tests/arrays/categorical/test_missing.py b/pandas/tests/arrays/categorical/test_missing.py index 9eb3c8b3a8c48..33dd0cd04e0b6 100644 --- a/pandas/tests/arrays/categorical/test_missing.py +++ b/pandas/tests/arrays/categorical/test_missing.py @@ -5,7 +5,8 @@ from pandas.core.dtypes.dtypes import CategoricalDtype -from pandas import Categorical, Index, Series, isna +import pandas as pd +from pandas import Categorical, DataFrame, Index, Series, isna import pandas._testing as tm @@ -97,3 +98,26 @@ def test_fillna_array(self): expected = Categorical(["A", "B", "C", "B", "A"], dtype=cat.dtype) tm.assert_categorical_equal(result, expected) assert isna(cat[-1]) # didnt modify original inplace + + @pytest.mark.parametrize( + "values, expected", + [ + ([1, 2, 3], np.array([False, False, False])), + ([1, 2, np.nan], np.array([False, False, True])), + ([1, 2, np.inf], np.array([False, False, True])), + ], + ) + def test_use_inf_as_na(self, values, expected): + # https://github.com/pandas-dev/pandas/issues/33594 + with pd.option_context("mode.use_inf_as_na", True): + cat = Categorical(values) + result = cat.isna() + tm.assert_numpy_array_equal(result, expected) + + result = Series(cat).isna() + expected = Series(expected) + tm.assert_series_equal(result, expected) + + result = DataFrame(cat).isna() + expected = DataFrame(expected) + tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/arrays/string_/test_string.py b/pandas/tests/arrays/string_/test_string.py index fe770eed84b62..208c5ced57158 100644 --- a/pandas/tests/arrays/string_/test_string.py +++ b/pandas/tests/arrays/string_/test_string.py @@ -277,3 +277,25 @@ def test_value_counts_na(): result = arr.value_counts(dropna=True) expected = pd.Series([2, 1], index=["a", "b"], dtype="Int64") tm.assert_series_equal(result, expected) + + +@pytest.mark.parametrize( + "values, expected", + [ + (pd.array(["a", "b", "c"]), np.array([False, False, False])), + (pd.array(["a", "b", None]), np.array([False, False, True])), + ], +) +def test_use_na_as_inf(values, expected): + # https://github.com/pandas-dev/pandas/issues/33594 + with pd.option_context("mode.use_inf_as_na", True): + result = values.isna() + tm.assert_numpy_array_equal(result, expected) + + result = pd.Series(values).isna() + expected = pd.Series(expected) + tm.assert_series_equal(result, expected) + + result = pd.DataFrame(values).isna() + expected = pd.DataFrame(expected) + tm.assert_frame_equal(result, expected) From 55ed2cf2187df8f0e9c0ab42b3e0947f0477a2da Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Sat, 18 Apr 2020 09:29:48 -0500 Subject: [PATCH 02/15] Note --- doc/source/whatsnew/v1.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index e9696a11fc148..e012359d88e4d 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -637,7 +637,7 @@ ExtensionArray ^^^^^^^^^^^^^^ - Fixed bug where :meth:`Serires.value_counts` would raise on empty input of ``Int64`` dtype (:issue:`33317`) -- Fixed bug where :class:`StringArray` would not return ``True`` for NA values when ``pandas.options.mode.use_inf_as_na`` was set to ``True`` +- Fixed bug where :meth:`StringArray.isna` would not return ``False`` for NA values when ``pandas.options.mode.use_inf_as_na`` was set to ``True`` Other From 9582927e94e62355b83eb8de2f7a23a87988f907 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Sat, 18 Apr 2020 09:31:28 -0500 Subject: [PATCH 03/15] Issue number --- doc/source/whatsnew/v1.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index e012359d88e4d..0dbc11fd2a5a8 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -637,7 +637,7 @@ ExtensionArray ^^^^^^^^^^^^^^ - Fixed bug where :meth:`Serires.value_counts` would raise on empty input of ``Int64`` dtype (:issue:`33317`) -- Fixed bug where :meth:`StringArray.isna` would not return ``False`` for NA values when ``pandas.options.mode.use_inf_as_na`` was set to ``True`` +- Fixed bug where :meth:`StringArray.isna` would not return ``False`` for NA values when ``pandas.options.mode.use_inf_as_na`` was set to ``True`` (:issue:`33594`) Other From 365df7e9754100b8188dedd5a682f526eab28150 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Sat, 18 Apr 2020 15:09:00 -0500 Subject: [PATCH 04/15] Oops --- doc/source/whatsnew/v1.1.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 0dbc11fd2a5a8..09c37b059651e 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -637,7 +637,7 @@ ExtensionArray ^^^^^^^^^^^^^^ - Fixed bug where :meth:`Serires.value_counts` would raise on empty input of ``Int64`` dtype (:issue:`33317`) -- Fixed bug where :meth:`StringArray.isna` would not return ``False`` for NA values when ``pandas.options.mode.use_inf_as_na`` was set to ``True`` (:issue:`33594`) +- Fixed bug where :meth:`StringArray.isna` would return ``False`` for NA values when ``pandas.options.mode.use_inf_as_na`` was set to ``True`` (:issue:`33594`) Other From 1d0514556e3cf8d7df1da02994df1fb6ba935a59 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Sat, 18 Apr 2020 21:38:02 -0500 Subject: [PATCH 05/15] Different check --- pandas/_libs/missing.pyx | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/pandas/_libs/missing.pyx b/pandas/_libs/missing.pyx index b2c95b507eaee..a2a796d0ac98d 100644 --- a/pandas/_libs/missing.pyx +++ b/pandas/_libs/missing.pyx @@ -161,11 +161,7 @@ def isnaobj_old(arr: ndarray) -> ndarray: result = np.zeros(n, dtype=np.uint8) for i in range(n): val = arr[i] - result[i] = ( - val is NaT - or _check_none_nan_inf_neginf(val) - or val is C_NA - ) + result[i] = checknull(val) or val == INF or val == NEGINF return result.view(np.bool_) From c7c03b8da49b9bb068e3e8acb4caad0d4c3d49c6 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Sun, 19 Apr 2020 10:48:23 -0500 Subject: [PATCH 06/15] Remove StringArray stuff --- doc/source/whatsnew/v1.1.0.rst | 6 ++---- pandas/_libs/missing.pyx | 3 +-- pandas/tests/arrays/string_/test_string.py | 22 ---------------------- 3 files changed, 3 insertions(+), 28 deletions(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 334a611e01a52..16be7e8ebab82 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -182,9 +182,7 @@ Other API changes Backwards incompatible API changes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - :meth:`DataFrame.swaplevels` now raises a ``TypeError`` if the axis is not a :class:`MultiIndex`. - Previously an ``AttributeError`` was raised (:issue:`31126`) -- :meth:`DataFrame.xs` now raises a ``TypeError`` if a ``level`` keyword is supplied and the axis is not a :class:`MultiIndex`. - Previously an ``AttributeError`` was raised (:issue:`33610`) + Previously a ``AttributeError`` was raised (:issue:`31126`) - :meth:`DataFrameGroupby.mean` and :meth:`SeriesGroupby.mean` (and similarly for :meth:`~DataFrameGroupby.median`, :meth:`~DataFrameGroupby.std` and :meth:`~DataFrameGroupby.var`) now raise a ``TypeError`` if a not-accepted keyword argument is passed into it. Previously a ``UnsupportedFunctionCall`` was raised (``AssertionError`` if ``min_count`` passed into :meth:`~DataFrameGroupby.median`) (:issue:`31485`) @@ -639,7 +637,7 @@ ExtensionArray ^^^^^^^^^^^^^^ - Fixed bug where :meth:`Serires.value_counts` would raise on empty input of ``Int64`` dtype (:issue:`33317`) -- Fixed bug where :meth:`StringArray.isna` would return ``False`` for NA values when ``pandas.options.mode.use_inf_as_na`` was set to ``True`` (:issue:`33594`) +- Other diff --git a/pandas/_libs/missing.pyx b/pandas/_libs/missing.pyx index a2a796d0ac98d..dacf454824190 100644 --- a/pandas/_libs/missing.pyx +++ b/pandas/_libs/missing.pyx @@ -140,7 +140,6 @@ def isnaobj_old(arr: ndarray) -> ndarray: - INF - NEGINF - NaT - - NA Parameters ---------- @@ -161,7 +160,7 @@ def isnaobj_old(arr: ndarray) -> ndarray: result = np.zeros(n, dtype=np.uint8) for i in range(n): val = arr[i] - result[i] = checknull(val) or val == INF or val == NEGINF + result[i] = val is NaT or _check_none_nan_inf_neginf(val) return result.view(np.bool_) diff --git a/pandas/tests/arrays/string_/test_string.py b/pandas/tests/arrays/string_/test_string.py index 208c5ced57158..fe770eed84b62 100644 --- a/pandas/tests/arrays/string_/test_string.py +++ b/pandas/tests/arrays/string_/test_string.py @@ -277,25 +277,3 @@ def test_value_counts_na(): result = arr.value_counts(dropna=True) expected = pd.Series([2, 1], index=["a", "b"], dtype="Int64") tm.assert_series_equal(result, expected) - - -@pytest.mark.parametrize( - "values, expected", - [ - (pd.array(["a", "b", "c"]), np.array([False, False, False])), - (pd.array(["a", "b", None]), np.array([False, False, True])), - ], -) -def test_use_na_as_inf(values, expected): - # https://github.com/pandas-dev/pandas/issues/33594 - with pd.option_context("mode.use_inf_as_na", True): - result = values.isna() - tm.assert_numpy_array_equal(result, expected) - - result = pd.Series(values).isna() - expected = pd.Series(expected) - tm.assert_series_equal(result, expected) - - result = pd.DataFrame(values).isna() - expected = pd.DataFrame(expected) - tm.assert_frame_equal(result, expected) From dbf9830192be66c54604c0c6c61b06f1ab77b1df Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Sun, 19 Apr 2020 10:52:50 -0500 Subject: [PATCH 07/15] Fix --- doc/source/whatsnew/v1.1.0.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 16be7e8ebab82..d99a203936ba0 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -182,7 +182,9 @@ Other API changes Backwards incompatible API changes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - :meth:`DataFrame.swaplevels` now raises a ``TypeError`` if the axis is not a :class:`MultiIndex`. - Previously a ``AttributeError`` was raised (:issue:`31126`) + Previously an ``AttributeError`` was raised (:issue:`31126`) +- :meth:`DataFrame.xs` now raises a ``TypeError`` if a ``level`` keyword is supplied and the axis is not a :class:`MultiIndex`. + Previously an ``AttributeError`` was raised (:issue:`33610`) - :meth:`DataFrameGroupby.mean` and :meth:`SeriesGroupby.mean` (and similarly for :meth:`~DataFrameGroupby.median`, :meth:`~DataFrameGroupby.std` and :meth:`~DataFrameGroupby.var`) now raise a ``TypeError`` if a not-accepted keyword argument is passed into it. Previously a ``UnsupportedFunctionCall`` was raised (``AssertionError`` if ``min_count`` passed into :meth:`~DataFrameGroupby.median`) (:issue:`31485`) From 60c762508295d7e27e65aaab4ace593f78ae5ed7 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Sun, 19 Apr 2020 20:32:45 -0500 Subject: [PATCH 08/15] Update --- pandas/core/dtypes/missing.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/pandas/core/dtypes/missing.py b/pandas/core/dtypes/missing.py index 949842afac6a0..9f6d9e5afab7c 100644 --- a/pandas/core/dtypes/missing.py +++ b/pandas/core/dtypes/missing.py @@ -231,16 +231,13 @@ def _isna_ndarraylike(obj): def _isna_ndarraylike_old(obj): - if not isinstance(obj, np.ndarray): - values = obj.to_numpy() - else: - values = obj - + values = getattr(obj, "_values", obj) dtype = values.dtype - if is_string_dtype(dtype): + if is_extension_array_dtype(dtype): + result = values.isna() | values.isin([-np.inf, np.inf]) + elif is_string_dtype(dtype): result = _isna_string_dtype(values, dtype, old=True) - elif needs_i8_conversion(dtype): # this is the NaT pattern result = values.view("i8") == iNaT From ddff3d286908c1b5469197559d5261e1922723aa Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Sun, 19 Apr 2020 20:53:11 -0500 Subject: [PATCH 09/15] Use equality instead --- pandas/core/dtypes/missing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/dtypes/missing.py b/pandas/core/dtypes/missing.py index a64e262d28f9d..04d12d999afab 100644 --- a/pandas/core/dtypes/missing.py +++ b/pandas/core/dtypes/missing.py @@ -234,7 +234,7 @@ def _isna_ndarraylike_old(obj): dtype = values.dtype if is_extension_array_dtype(dtype): - result = values.isna() | values.isin([-np.inf, np.inf]) + result = values.isna() | (values == -np.inf) | (values == np.inf) elif is_string_dtype(dtype): result = _isna_string_dtype(values, dtype, old=True) elif needs_i8_conversion(dtype): From cbfdd6a71783f9dae35d1bd9d7a66b9bf69fc4d2 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Tue, 21 Apr 2020 14:13:54 -0500 Subject: [PATCH 10/15] Delete _isna_ndarraylike_old --- pandas/core/dtypes/missing.py | 48 ++++++++++++----------------------- 1 file changed, 16 insertions(+), 32 deletions(-) diff --git a/pandas/core/dtypes/missing.py b/pandas/core/dtypes/missing.py index 04d12d999afab..b3ed1ff71967e 100644 --- a/pandas/core/dtypes/missing.py +++ b/pandas/core/dtypes/missing.py @@ -134,13 +134,13 @@ def _isna_new(obj): elif isinstance(obj, type): return False elif isinstance(obj, (ABCSeries, np.ndarray, ABCIndexClass, ABCExtensionArray)): - return _isna_ndarraylike(obj) + return _isna_ndarraylike(obj, old=False) elif isinstance(obj, ABCDataFrame): return obj.isna() elif isinstance(obj, list): - return _isna_ndarraylike(np.asarray(obj, dtype=object)) + return _isna_ndarraylike(np.asarray(obj, dtype=object), old=False) elif hasattr(obj, "__array__"): - return _isna_ndarraylike(np.asarray(obj)) + return _isna_ndarraylike(np.asarray(obj), old=False) else: return False @@ -165,13 +165,13 @@ def _isna_old(obj): elif isinstance(obj, type): return False elif isinstance(obj, (ABCSeries, np.ndarray, ABCIndexClass, ABCExtensionArray)): - return _isna_ndarraylike_old(obj) + return _isna_ndarraylike(obj, old=True) elif isinstance(obj, ABCDataFrame): return obj.isna() elif isinstance(obj, list): - return _isna_ndarraylike_old(np.asarray(obj, dtype=object)) + return _isna_ndarraylike(np.asarray(obj, dtype=object), old=True) elif hasattr(obj, "__array__"): - return _isna_ndarraylike_old(np.asarray(obj)) + return _isna_ndarraylike(np.asarray(obj), old=True) else: return False @@ -207,41 +207,25 @@ def _use_inf_as_na(key): globals()["_isna"] = _isna_new -def _isna_ndarraylike(obj): +def _isna_ndarraylike(obj, old=False): values = getattr(obj, "_values", obj) dtype = values.dtype if is_extension_array_dtype(dtype): - result = values.isna() - elif is_string_dtype(dtype): - result = _isna_string_dtype(values, dtype, old=False) - - elif needs_i8_conversion(dtype): - # this is the NaT pattern - result = values.view("i8") == iNaT - else: - result = np.isnan(values) - - # box - if isinstance(obj, ABCSeries): - result = obj._constructor(result, index=obj.index, name=obj.name, copy=False) - - return result - - -def _isna_ndarraylike_old(obj): - values = getattr(obj, "_values", obj) - dtype = values.dtype - - if is_extension_array_dtype(dtype): - result = values.isna() | (values == -np.inf) | (values == np.inf) + if old: + result = values.isna() | (values == -np.inf) | (values == np.inf) + else: + result = values.isna() elif is_string_dtype(dtype): - result = _isna_string_dtype(values, dtype, old=True) + result = _isna_string_dtype(values, dtype, old=old) elif needs_i8_conversion(dtype): # this is the NaT pattern result = values.view("i8") == iNaT else: - result = ~np.isfinite(values) + if old: + result = ~np.isfinite(values) + else: + result = np.isnan(values) # box if isinstance(obj, ABCSeries): From 1838a330e3792323623fad1525d1d1ad257eee67 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Tue, 21 Apr 2020 15:25:51 -0500 Subject: [PATCH 11/15] Add some tests --- .../tests/arrays/categorical/test_missing.py | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/pandas/tests/arrays/categorical/test_missing.py b/pandas/tests/arrays/categorical/test_missing.py index 33dd0cd04e0b6..3e9d90bd8c710 100644 --- a/pandas/tests/arrays/categorical/test_missing.py +++ b/pandas/tests/arrays/categorical/test_missing.py @@ -105,6 +105,7 @@ def test_fillna_array(self): ([1, 2, 3], np.array([False, False, False])), ([1, 2, np.nan], np.array([False, False, True])), ([1, 2, np.inf], np.array([False, False, True])), + ([1, 2, pd.NA], np.array([False, False, True])), ], ) def test_use_inf_as_na(self, values, expected): @@ -121,3 +122,35 @@ def test_use_inf_as_na(self, values, expected): result = DataFrame(cat).isna() expected = DataFrame(expected) tm.assert_frame_equal(result, expected) + + @pytest.mark.parametrize( + "values, expected", + [ + ([1, 2, 3], np.array([False, False, False])), + ([1, 2, np.nan], np.array([False, False, True])), + ([1, 2, np.inf], np.array([False, False, True])), + ([1, 2, pd.NA], np.array([False, False, True])), + ], + ) + def test_use_inf_as_na_outside_context(self, values, expected): + # https://github.com/pandas-dev/pandas/issues/33594 + # Using isna directly for Categorical will fail in general here + cat = Categorical(values) + + with pd.option_context("mode.use_inf_as_na", True): + result = pd.isna(cat) + tm.assert_numpy_array_equal(result, expected) + + cat = pd.Series(Categorical(values)) + + with pd.option_context("mode.use_inf_as_na", True): + result = pd.isna(cat) + expected = Series(expected) + tm.assert_series_equal(result, expected) + + cat = pd.DataFrame(Categorical(values)) + + with pd.option_context("mode.use_inf_as_na", True): + result = pd.isna(cat) + expected = DataFrame(expected) + tm.assert_frame_equal(result, expected) From 11d50a9b9af7af6196cb7e79a59ab9f9a25fe215 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Tue, 21 Apr 2020 16:21:04 -0500 Subject: [PATCH 12/15] Fix test --- pandas/tests/arrays/categorical/test_missing.py | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/pandas/tests/arrays/categorical/test_missing.py b/pandas/tests/arrays/categorical/test_missing.py index 3e9d90bd8c710..5309b8827e3f0 100644 --- a/pandas/tests/arrays/categorical/test_missing.py +++ b/pandas/tests/arrays/categorical/test_missing.py @@ -141,16 +141,10 @@ def test_use_inf_as_na_outside_context(self, values, expected): result = pd.isna(cat) tm.assert_numpy_array_equal(result, expected) - cat = pd.Series(Categorical(values)) - - with pd.option_context("mode.use_inf_as_na", True): - result = pd.isna(cat) + result = pd.isna(Series(cat)) expected = Series(expected) tm.assert_series_equal(result, expected) - cat = pd.DataFrame(Categorical(values)) - - with pd.option_context("mode.use_inf_as_na", True): - result = pd.isna(cat) + result = pd.isna(DataFrame(cat)) expected = DataFrame(expected) tm.assert_frame_equal(result, expected) From afa944852f23e96db9af26b35e9e5dfaa592404b Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Sun, 26 Apr 2020 17:08:00 -0500 Subject: [PATCH 13/15] Add docstring --- pandas/core/dtypes/missing.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/pandas/core/dtypes/missing.py b/pandas/core/dtypes/missing.py index b3ed1ff71967e..cbe2ef8e37041 100644 --- a/pandas/core/dtypes/missing.py +++ b/pandas/core/dtypes/missing.py @@ -207,7 +207,24 @@ def _use_inf_as_na(key): globals()["_isna"] = _isna_new -def _isna_ndarraylike(obj, old=False): +def _isna_ndarraylike(obj, old: str = False): + """ + Return an array indicating which values of the input + array are NaN / NA. + + Parameters + ---------- + obj: array-like + The input array whose elements are to be checked. + old: bool + Whether or not to treat infinite values as NA. + + Returns + ------- + array-like + Array of boolean values denoting the NA status of + each element. + """ values = getattr(obj, "_values", obj) dtype = values.dtype From a95f38c2b501d29c02a26849e73b53aebd447471 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Sun, 26 Apr 2020 19:08:12 -0500 Subject: [PATCH 14/15] Fix --- pandas/core/dtypes/missing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/dtypes/missing.py b/pandas/core/dtypes/missing.py index cbe2ef8e37041..bd639a6b82f2b 100644 --- a/pandas/core/dtypes/missing.py +++ b/pandas/core/dtypes/missing.py @@ -207,7 +207,7 @@ def _use_inf_as_na(key): globals()["_isna"] = _isna_new -def _isna_ndarraylike(obj, old: str = False): +def _isna_ndarraylike(obj, old: bool = False): """ Return an array indicating which values of the input array are NaN / NA. From fe1648f3378fc57cb3c976b4369b6705ba379753 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Sun, 26 Apr 2020 19:11:08 -0500 Subject: [PATCH 15/15] Nit --- pandas/core/dtypes/missing.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pandas/core/dtypes/missing.py b/pandas/core/dtypes/missing.py index bd639a6b82f2b..92e1b17c41694 100644 --- a/pandas/core/dtypes/missing.py +++ b/pandas/core/dtypes/missing.py @@ -209,8 +209,7 @@ def _use_inf_as_na(key): def _isna_ndarraylike(obj, old: bool = False): """ - Return an array indicating which values of the input - array are NaN / NA. + Return an array indicating which values of the input array are NaN / NA. Parameters ---------- @@ -222,8 +221,7 @@ def _isna_ndarraylike(obj, old: bool = False): Returns ------- array-like - Array of boolean values denoting the NA status of - each element. + Array of boolean values denoting the NA status of each element. """ values = getattr(obj, "_values", obj) dtype = values.dtype