Skip to content

Commit 5a9a9b1

Browse files
Backport PR #47326 on branch 1.4.x (REGR: Fix nan comparison for same Index object) (#47364)
Backport PR #47326: REGR: Fix nan comparison for same Index object Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 17386d1 commit 5a9a9b1

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

doc/source/whatsnew/v1.4.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Fixed regressions
2020
- Fixed regression in :func:`read_fwf` raising ``ValueError`` when ``widths`` was specified with ``usecols`` (:issue:`46580`)
2121
- Fixed regression in :func:`concat` not sorting columns for mixed column names (:issue:`47127`)
2222
- Fixed regression in :meth:`.Groupby.transform` and :meth:`.Groupby.agg` failing with ``engine="numba"`` when the index was a :class:`MultiIndex` (:issue:`46867`)
23+
- Fixed regression in ``NaN`` comparison for :class:`Index` operations where the same object was compared (:issue:`47105`)
2324
- Fixed regression is :meth:`.Styler.to_latex` and :meth:`.Styler.to_html` where ``buf`` failed in combination with ``encoding`` (:issue:`47053`)
2425
- Fixed regression in :func:`read_csv` with ``index_col=False`` identifying first row as index names when ``header=None`` (:issue:`46955`)
2526
- Fixed regression in :meth:`.DataFrameGroupBy.agg` when used with list-likes or dict-likes and ``axis=1`` that would give incorrect results; now raises ``NotImplementedError`` (:issue:`46995`)

pandas/core/indexes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6660,7 +6660,7 @@ def _cmp_method(self, other, op):
66606660
# TODO: should set MultiIndex._can_hold_na = False?
66616661
arr[self.isna()] = False
66626662
return arr
6663-
elif op in {operator.ne, operator.lt, operator.gt}:
6663+
elif op is operator.ne:
66646664
arr = np.zeros(len(self), dtype=bool)
66656665
if self._can_hold_na and not isinstance(self, ABCMultiIndex):
66666666
arr[self.isna()] = True

pandas/tests/indexes/test_base.py

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from datetime import datetime
33
from io import StringIO
44
import math
5+
import operator
56
import re
67

78
import numpy as np
@@ -1587,3 +1588,16 @@ def test_get_attributes_dict_deprecated():
15871588
with tm.assert_produces_warning(DeprecationWarning):
15881589
attrs = idx._get_attributes_dict()
15891590
assert attrs == {"name": None}
1591+
1592+
1593+
@pytest.mark.parametrize("op", [operator.lt, operator.gt])
1594+
def test_nan_comparison_same_object(op):
1595+
# GH#47105
1596+
idx = Index([np.nan])
1597+
expected = np.array([False])
1598+
1599+
result = op(idx, idx)
1600+
tm.assert_numpy_array_equal(result, expected)
1601+
1602+
result = op(idx, idx.copy())
1603+
tm.assert_numpy_array_equal(result, expected)

0 commit comments

Comments
 (0)