Skip to content

Commit

Permalink
BUG: FloatingArray.__contains__(nan) (#52840)
Browse files Browse the repository at this point in the history
* BUG: FloatingArray.__contains__(nan)

* GH ref

* mypy fixup

* mypy fixup

---------

Co-authored-by: Patrick Hoefler <61934744+phofl@users.noreply.github.com>
  • Loading branch information
jbrockmendel and phofl committed May 1, 2023
1 parent 000c3ed commit f886f13
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
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 @@ -432,6 +432,7 @@ Metadata

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

0 comments on commit f886f13

Please sign in to comment.