Skip to content

Commit 9e948e6

Browse files
jbrockmendelphofl
authored and
im-vinicius
committed
BUG: FloatingArray.__contains__(nan) (pandas-dev#52840)
* BUG: FloatingArray.__contains__(nan) * GH ref * mypy fixup * mypy fixup --------- Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 4cb91de commit 9e948e6

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ Metadata
432432

433433
Other
434434
^^^^^
435+
- Bug in :class:`FloatingArray.__contains__` with ``NaN`` item incorrectly returning ``False`` when ``NaN`` values are presnet (:issue:`52840`)
435436
- Bug in :func:`assert_almost_equal` now throwing assertion error for two unequal sets (:issue:`51727`)
436437
- Bug in :func:`assert_frame_equal` checks category dtypes even when asked not to check index type (:issue:`52126`)
437438
- Bug in :meth:`DataFrame.reindex` with a ``fill_value`` that should be inferred with a :class:`ExtensionDtype` incorrectly inferring ``object`` dtype (:issue:`52586`)

pandas/core/arrays/arrow/array.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ def __len__(self) -> int:
578578
def __contains__(self, key) -> bool:
579579
# https://github.com/pandas-dev/pandas/pull/51307#issuecomment-1426372604
580580
if isna(key) and key is not self.dtype.na_value:
581-
if self.dtype.kind == "f" and lib.is_float(key) and isna(key):
581+
if self.dtype.kind == "f" and lib.is_float(key):
582582
return pc.any(pc.is_nan(self._pa_array)).as_py()
583583

584584
# e.g. date or timestamp types we do not allow None here to match pd.NA

pandas/core/arrays/masked.py

+8
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,14 @@ def __setitem__(self, key, value) -> None:
236236
self._data[key] = value
237237
self._mask[key] = mask
238238

239+
def __contains__(self, key) -> bool:
240+
if isna(key) and key is not self.dtype.na_value:
241+
# GH#52840
242+
if self._data.dtype.kind == "f" and lib.is_float(key):
243+
return bool((np.isnan(self._data) & ~self._mask).any())
244+
245+
return bool(super().__contains__(key))
246+
239247
def __iter__(self) -> Iterator:
240248
if self.ndim == 1:
241249
if not self._hasna:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import numpy as np
2+
3+
import pandas as pd
4+
5+
6+
def test_contains_nan():
7+
# GH#52840
8+
arr = pd.array(range(5)) / 0
9+
10+
assert np.isnan(arr._data[0])
11+
assert not arr.isna()[0]
12+
assert np.nan in arr

0 commit comments

Comments
 (0)