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: nan comparisons behave differently on identical objects #47105

Closed
2 of 3 tasks
vyasr opened this issue May 24, 2022 · 3 comments · Fixed by #47326
Closed
2 of 3 tasks

BUG: nan comparisons behave differently on identical objects #47105

vyasr opened this issue May 24, 2022 · 3 comments · Fixed by #47326
Labels
Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Numeric Operations Arithmetic, Comparison, and Logical operations Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@vyasr
Copy link
Contributor

vyasr commented May 24, 2022

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

>>> import pandas as pd
>>> import numpy as np
>>> idx = pd.Index([np.nan])
>>> idx > idx
array([ True])
>>> idx > idx.copy()
array([False])

Issue Description

When an index contains np.nan, it compares differently to itself than to any other index object containing np.nan (including a copy of itself). This seems likely to be caused by some internal check for identity (other is self) that is taking precedence over element-wise comparisons.

Expected Behavior

NaN should follow IEEE 754, so we should see

>>> idx > idx
array([False])

without needing a copy.

Installed Versions

INSTALLED VERSIONS

commit : 4bfe3d0
python : 3.8.13.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-76-generic
Version : #86-Ubuntu SMP Fri Jan 17 17:24:28 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_US.UTF-8
pandas : 1.4.2
numpy : 1.21.6
pytz : 2022.1
dateutil : 2.8.2
pip : 22.1.1
setuptools : 62.3.2
Cython : 0.29.30
pytest : 7.1.2
hypothesis : 6.46.7
sphinx : 4.5.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.8.0
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 8.3.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : None
brotli :
fastparquet : None
fsspec : 2022.5.0
gcsfs : None
markupsafe : 2.1.1
matplotlib : None
numba : 0.55.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 7.0.0
pyreadstat : None
pyxlsb : None
s3fs : 2022.5.0
scipy : 1.8.1
snappy :
sqlalchemy : 1.4.36
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None

@vyasr vyasr added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 24, 2022
@mroeschke mroeschke added Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Numeric Operations Arithmetic, Comparison, and Logical operations and removed Needs Triage Issue that has not been reviewed by a pandas team member labels May 24, 2022
@mroeschke
Copy link
Member

cc @jbrockmendel possibly traced back to #43555

simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue May 24, 2022
@simonjayhawkins simonjayhawkins added the Regression Functionality that used to work in a prior pandas version label May 24, 2022
@simonjayhawkins simonjayhawkins added this to the 1.4.3 milestone May 24, 2022
@simonjayhawkins
Copy link
Member

cc @jbrockmendel possibly traced back to #43555

can confirm. first bad commit: [e9d0a58] REF: Share Index comparison and arithmetic methods (#43555)

@jbrockmendel
Copy link
Member

Yah, looks like the trouble is in

            elif op in {operator.ne, operator.lt, operator.gt}:
                arr = np.zeros(len(self), dtype=bool)
                if self._can_hold_na and not isinstance(self, ABCMultiIndex):
                    arr[self.isna()] = True
                return arr

the setting arr[self.isna()] = True should only be for operator.ne

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Numeric Operations Arithmetic, Comparison, and Logical operations Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants