Skip to content

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

Closed
@DomKennedy

Description

@DomKennedy

  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugRegressionFunctionality that used to work in a prior pandas version

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions