Skip to content
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

BUG: FloatingArray.__contains__(nan) #52840

Merged
merged 7 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ Styler

Other
^^^^^
- Bug in :class:`FloatingArray.__contains__` with ``NaN`` item incorrectly returning ``False`` when ``NaN`` values are presnet (:issue:`52840`)
- Bug in :func:`assert_almost_equal` now throwing assertion error for two unequal sets (:issue:`51727`)
- Bug in :func:`assert_frame_equal` checks category dtypes even when asked not to check index type (:issue:`52126`)
- Bug in :meth:`DataFrame.reindex` with a ``fill_value`` that should be inferred with a :class:`ExtensionDtype` incorrectly inferring ``object`` dtype (:issue:`52586`)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/arrow/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ def __len__(self) -> int:
def __contains__(self, key) -> bool:
# https://github.com/pandas-dev/pandas/pull/51307#issuecomment-1426372604
if isna(key) and key is not self.dtype.na_value:
if self.dtype.kind == "f" and lib.is_float(key) and isna(key):
if self.dtype.kind == "f" and lib.is_float(key):
return pc.any(pc.is_nan(self._pa_array)).as_py()

# e.g. date or timestamp types we do not allow None here to match pd.NA
Expand Down
8 changes: 8 additions & 0 deletions pandas/core/arrays/masked.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,14 @@ def __setitem__(self, key, value) -> None:
self._data[key] = value
self._mask[key] = mask

def __contains__(self, key) -> bool:
if isna(key) and key is not self.dtype.na_value:
# GH#52840
if self._data.dtype.kind == "f" and lib.is_float(key):
return bool((np.isnan(self._data) & ~self._mask).any())

return bool(super().__contains__(key))

def __iter__(self) -> Iterator:
if self.ndim == 1:
if not self._hasna:
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/arrays/floating/test_contains.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import numpy as np

import pandas as pd


def test_contains_nan():
# GH#52840
arr = pd.array(range(5)) / 0

assert np.isnan(arr._data[0])
assert not arr.isna()[0]
assert np.nan in arr
Loading