Skip to content

Commit 60e47f5

Browse files
Backport PR #33629 on branch 1.0.x (BUG: Fix Categorical use_inf_as_na bug) (#34001)
Co-authored-by: Daniel Saxton <[email protected]>
1 parent 988c5d4 commit 60e47f5

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

doc/source/whatsnew/v1.0.4.rst

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ including other versions of pandas.
1515

1616
Fixed regressions
1717
~~~~~~~~~~~~~~~~~
18+
- 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`)
1819
- Bug in :meth:`GroupBy.first` and :meth:`GroupBy.last` where None is not preserved in object dtype (:issue:`32800`)
1920
- Bug in DataFrame reductions using ``numeric_only=True`` and ExtensionArrays (:issue:`33256`).
2021
- Bug where :meth:`Categorical.replace` would replace with ``NaN`` whenever the new value and replacement value were equal (:issue:`33288`)

pandas/core/dtypes/missing.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,18 @@ def _isna_ndarraylike(obj):
269269

270270

271271
def _isna_ndarraylike_old(obj):
272+
is_extension = is_extension_array_dtype(obj)
273+
272274
values = getattr(obj, "values", obj)
273275
dtype = values.dtype
274276

275-
if is_string_dtype(dtype):
277+
if is_extension:
278+
if isinstance(obj, (ABCIndexClass, ABCSeries)):
279+
values = obj._values
280+
else:
281+
values = obj
282+
result = values.isna() | (values == -np.inf) | (values == np.inf)
283+
elif is_string_dtype(dtype):
276284
# Working around NumPy ticket 1542
277285
shape = values.shape
278286

pandas/tests/arrays/categorical/test_missing.py

+52-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66
from pandas.core.dtypes.dtypes import CategoricalDtype
77

8-
from pandas import Categorical, Index, Series, isna
8+
import pandas as pd
9+
from pandas import Categorical, DataFrame, Index, Series, isna
910
import pandas._testing as tm
1011

1112

@@ -82,3 +83,53 @@ def test_fillna_iterable_category(self, named):
8283
expected = Categorical([Point(0, 0), Point(0, 1), Point(0, 0)])
8384

8485
tm.assert_categorical_equal(result, expected)
86+
87+
@pytest.mark.parametrize(
88+
"values, expected",
89+
[
90+
([1, 2, 3], np.array([False, False, False])),
91+
([1, 2, np.nan], np.array([False, False, True])),
92+
([1, 2, np.inf], np.array([False, False, True])),
93+
([1, 2, pd.NA], np.array([False, False, True])),
94+
],
95+
)
96+
def test_use_inf_as_na(self, values, expected):
97+
# https://github.com/pandas-dev/pandas/issues/33594
98+
with pd.option_context("mode.use_inf_as_na", True):
99+
cat = Categorical(values)
100+
result = cat.isna()
101+
tm.assert_numpy_array_equal(result, expected)
102+
103+
result = Series(cat).isna()
104+
expected = Series(expected)
105+
tm.assert_series_equal(result, expected)
106+
107+
result = DataFrame(cat).isna()
108+
expected = DataFrame(expected)
109+
tm.assert_frame_equal(result, expected)
110+
111+
@pytest.mark.parametrize(
112+
"values, expected",
113+
[
114+
([1, 2, 3], np.array([False, False, False])),
115+
([1, 2, np.nan], np.array([False, False, True])),
116+
([1, 2, np.inf], np.array([False, False, True])),
117+
([1, 2, pd.NA], np.array([False, False, True])),
118+
],
119+
)
120+
def test_use_inf_as_na_outside_context(self, values, expected):
121+
# https://github.com/pandas-dev/pandas/issues/33594
122+
# Using isna directly for Categorical will fail in general here
123+
cat = Categorical(values)
124+
125+
with pd.option_context("mode.use_inf_as_na", True):
126+
result = pd.isna(cat)
127+
tm.assert_numpy_array_equal(result, expected)
128+
129+
result = pd.isna(Series(cat))
130+
expected = Series(expected)
131+
tm.assert_series_equal(result, expected)
132+
133+
result = pd.isna(DataFrame(cat))
134+
expected = DataFrame(expected)
135+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)