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: .equals method returns true when comparing floats with dtype object to None #44190

Closed
3 tasks done
DomKennedy opened this issue Oct 26, 2021 · 2 comments · Fixed by #44195
Closed
3 tasks done

BUG: .equals method returns true when comparing floats with dtype object to None #44190

DomKennedy opened this issue Oct 26, 2021 · 2 comments · Fixed by #44195
Labels
Bug Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@DomKennedy
Copy link

  • 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 master branch of pandas.

Reproducible Example

import numpy as np
import pandas as pd

left = pd.Series([-np.inf, np.nan, -1.0, 0.0, 1.0, 10 / 3, np.inf], dtype=object)
right = pd.Series([None] * len(left))

print(pd.DataFrame(dict(left=left, right=right)))
print(f"{left.equals(right)=}")
print(f"{right.equals(left)=}")

Issue Description

If one constructs a Series left containing floating-point data, but declared as dtype object, and another Series right of the same length, containing only None, then left.equals.right returns True, even though the two series are demonstrably not equal.

Making the comparison the other way round (i.e. right.equals(right)) returns False as is semantically correct.

Some preliminary testing brings up the following conditions for the bug to occur:

  • left can consist of any combination of float values - including +/-inf, NaN, and both integral and fractional floats
  • If left contains values of any other type, the .equals call will correctly return False - this includes ints and None itself
  • The bug seems to occur with DataFrames as well - calling .to_frame() on both sides before calling .equals() has the same results.
  • The bug seems to have been introduced in pandas 1.3.0. I have observed it to occur in 1.3.0 and 1.3.4, and it does not occur on 1.2.5.
  • The bug occurs in both WSL and (ARM) macOS environments - I haven't tested on other platforms.

Expected Behavior

left.equals(right) should return False.

Installed Versions

INSTALLED VERSIONS

commit : 945c9ed
python : 3.9.7.final.0
python-bits : 64
OS : Linux
OS-release : 4.4.0-19041-Microsoft
Version : #1237-Microsoft Sat Sep 11 14:32:00 PST 2021
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.3.4
numpy : 1.21.3
pytz : 2021.3
dateutil : 2.8.2
pip : 20.3.4
setuptools : 58.2.0
Cython : 0.29.24
pytest : 6.2.5
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.2
IPython : 7.28.0
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.4.3
numexpr : 2.7.3
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 5.0.0
pyxlsb : None
s3fs : None
scipy : 1.7.1
sqlalchemy : 1.3.24
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@DomKennedy DomKennedy added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 26, 2021
@CloseChoice CloseChoice added Regression Functionality that used to work in a prior pandas version Internals Related to non-user accessible pandas implementation and removed Internals Related to non-user accessible pandas implementation labels Oct 26, 2021
@CloseChoice
Copy link
Member

Thanks for reporting, this is a clear bug. First of all this indicates that pd.Series.equals is not symmetric which is a problem, maybe we could use hypothesis tests to check this in the future?

Second, I investigated this a bit and the problem is in pd._lib.array_equivalent_object, see:

import pandas as pd
import numpy as np

left = np.array([-np.inf, np.nan, -1.0, 0.0, 1.0, 10 / 3, np.inf])
right = np.array([None] * len(left))
pd._lib.array_equivalent_object(left, right)  # results in True

@CloseChoice CloseChoice removed the Needs Triage Issue that has not been reviewed by a pandas team member label Oct 26, 2021
@jreback jreback modified the milestones: 1.4, 1.3.5 Oct 28, 2021
simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Oct 31, 2021
@simonjayhawkins
Copy link
Member

first bad commit: [602502e] BUG: equals/assert_numpy_array_equals with non-singleton NAs (#39650)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 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