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 7 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 @@ -731,9 +731,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
12 changes: 5 additions & 7 deletions pandas/core/dtypes/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,13 @@ def _isna_ndarraylike(obj, old: bool = False):
array-like
Array of boolean values denoting the NA status of each element.
"""
values = getattr(obj, "_values", obj)
if not isinstance(obj, np.ndarray):
values = obj.to_numpy()
else:
values = obj
dtype = values.dtype

if is_extension_array_dtype(dtype):
if old:
result = values.isna() | (values == -np.inf) | (values == np.inf)
else:
result = values.isna()
elif is_string_dtype(dtype):
if is_string_dtype(dtype):
result = _isna_string_dtype(values, dtype, old=old)
elif needs_i8_conversion(dtype):
# this is the NaT pattern
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_na_as_inf(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)