From 24a96239595e4a011544f0755ae61f387fb2800d Mon Sep 17 00:00:00 2001 From: phofl Date: Mon, 13 Jun 2022 10:33:03 +0200 Subject: [PATCH 1/3] REGR: Fix nan comparison for same Index object --- doc/source/whatsnew/v1.4.3.rst | 1 + pandas/core/indexes/base.py | 2 +- pandas/tests/indexes/test_base.py | 12 ++++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.3.rst b/doc/source/whatsnew/v1.4.3.rst index ca8b8ca15ec47..bfc9422711690 100644 --- a/doc/source/whatsnew/v1.4.3.rst +++ b/doc/source/whatsnew/v1.4.3.rst @@ -20,6 +20,7 @@ Fixed regressions - Fixed regression in :func:`read_fwf` raising ``ValueError`` when ``widths`` was specified with ``usecols`` (:issue:`46580`) - Fixed regression in :func:`concat` not sorting columns for mixed column names (:issue:`47127`) - Fixed regression in :meth:`.Groupby.transform` and :meth:`.Groupby.agg` failing with ``engine="numba"`` when the index was a :class:`MultiIndex` (:issue:`46867`) +- Fixed regression in ``NaN`` comparison for :class:`Index` operations where the same object was compared (:issue:`47105`) - Fixed regression is :meth:`.Styler.to_latex` and :meth:`.Styler.to_html` where ``buf`` failed in combination with ``encoding`` (:issue:`47053`) - Fixed regression in :func:`read_csv` with ``index_col=False`` identifying first row as index names when ``header=None`` (:issue:`46955`) - 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`) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index cb01cfc981739..08bd13492bf7c 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -6955,7 +6955,7 @@ def _cmp_method(self, other, op): # TODO: should set MultiIndex._can_hold_na = False? arr[self.isna()] = False return arr - elif op in {operator.ne, operator.lt, operator.gt}: + elif op == operator.ne: arr = np.zeros(len(self), dtype=bool) if self._can_hold_na and not isinstance(self, ABCMultiIndex): arr[self.isna()] = True diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 943cc945995a1..5d6a203d6da7d 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -1604,3 +1604,15 @@ def test_get_attributes_dict_deprecated(): with tm.assert_produces_warning(DeprecationWarning): attrs = idx._get_attributes_dict() assert attrs == {"name": None} + + +def test_nan_comparison_same_object(): + # GH#47105 + idx = Index([np.nan]) + expected = np.array([False]) + + result = idx > idx + tm.assert_numpy_array_equal(result, expected) + + result = idx > idx.copy() + tm.assert_numpy_array_equal(result, expected) From e8c814e2d9c39c872c59738d5bc0032c726c6ac9 Mon Sep 17 00:00:00 2001 From: phofl Date: Mon, 13 Jun 2022 12:09:56 +0200 Subject: [PATCH 2/3] Change equal --- pandas/core/indexes/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 08bd13492bf7c..01bbc424c3764 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -6955,7 +6955,7 @@ def _cmp_method(self, other, op): # TODO: should set MultiIndex._can_hold_na = False? arr[self.isna()] = False return arr - elif op == operator.ne: + elif op is operator.ne: arr = np.zeros(len(self), dtype=bool) if self._can_hold_na and not isinstance(self, ABCMultiIndex): arr[self.isna()] = True From 6ea033166ee7db48ba00e24bb18b1b77e11662f2 Mon Sep 17 00:00:00 2001 From: phofl Date: Tue, 14 Jun 2022 08:57:36 +0200 Subject: [PATCH 3/3] Add gt test --- pandas/tests/indexes/test_base.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 5d6a203d6da7d..5d7fc23feb5a8 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -2,6 +2,7 @@ from datetime import datetime from io import StringIO import math +import operator import re import numpy as np @@ -1606,13 +1607,14 @@ def test_get_attributes_dict_deprecated(): assert attrs == {"name": None} -def test_nan_comparison_same_object(): +@pytest.mark.parametrize("op", [operator.lt, operator.gt]) +def test_nan_comparison_same_object(op): # GH#47105 idx = Index([np.nan]) expected = np.array([False]) - result = idx > idx + result = op(idx, idx) tm.assert_numpy_array_equal(result, expected) - result = idx > idx.copy() + result = op(idx, idx.copy()) tm.assert_numpy_array_equal(result, expected)