Skip to content

BUG: Fix StringArray use_inf_as_na bug #33656

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 19 commits into from
May 10, 2020
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.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -732,9 +732,9 @@ ExtensionArray
^^^^^^^^^^^^^^

- Fixed bug where :meth:`Series.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:`33655`)
-


Other
^^^^^
- Appending a dictionary to a :class:`DataFrame` without passing ``ignore_index=True`` will raise ``TypeError: Can only append a dict if ignore_index=True``
Expand Down
8 changes: 2 additions & 6 deletions pandas/_libs/missing.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,6 @@ cpdef bint checknull_old(object val):
return False


cdef inline bint _check_none_nan_inf_neginf(object val):
return val is None or (isinstance(val, float) and
(val != val or val == INF or val == NEGINF))


@cython.wraparound(False)
@cython.boundscheck(False)
cpdef ndarray[uint8_t] isnaobj(ndarray arr):
Expand Down Expand Up @@ -141,6 +136,7 @@ def isnaobj_old(arr: ndarray) -> ndarray:
- INF
- NEGINF
- NaT
- NA

Parameters
----------
Expand All @@ -161,7 +157,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)
result[i] = checknull(val) or val == INF or val == NEGINF
return result.view(np.bool_)


Expand Down
2 changes: 1 addition & 1 deletion pandas/core/dtypes/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def _isna_ndarraylike(obj, inf_as_na: bool = False):

if is_extension_array_dtype(dtype):
if inf_as_na:
result = values.isna() | (values == -np.inf) | (values == np.inf)
result = libmissing.isnaobj_old(values.to_numpy())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we short-circuit this (and avoid the object-cast of to_numpy) for e.g. PeriodArray?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in general, but we could short-circuit the known built-in dtypes that will never be able to contain inf (basically all our internal EA dtypes except for categorical ?)

else:
result = values.isna()
elif is_string_dtype(dtype):
Expand Down
22 changes: 22 additions & 0 deletions pandas/tests/arrays/string_/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,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_inf_as_na(values, expected):
# https://github.com/pandas-dev/pandas/issues/33655
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)
7 changes: 7 additions & 0 deletions pandas/tests/extension/base/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,10 @@ def test_fillna_fill_other(self, data):
expected = pd.DataFrame({"A": data, "B": [0.0] * len(result)})

self.assert_frame_equal(result, expected)

def test_use_inf_as_na_no_effect(self, data_missing):
ser = pd.Series(data_missing)
expected = ser.isna()
with pd.option_context("mode.use_inf_as_na", True):
result = ser.isna()
self.assert_series_equal(result, expected)
6 changes: 3 additions & 3 deletions pandas/tests/series/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,12 +509,12 @@ def test_fillna_nat(self):
tm.assert_frame_equal(filled2, expected)

def test_isna_for_inf(self):
s = Series(["a", np.inf, np.nan, 1.0])
s = Series(["a", np.inf, np.nan, pd.NA, 1.0])
with pd.option_context("mode.use_inf_as_na", True):
r = s.isna()
dr = s.dropna()
e = Series([False, True, True, False])
de = Series(["a", 1.0], index=[0, 3])
e = Series([False, True, True, True, False])
de = Series(["a", 1.0], index=[0, 4])
tm.assert_series_equal(r, e)
tm.assert_series_equal(dr, de)

Expand Down