Skip to content

BUG: Inconsistent behavior in Index.difference #35217

Closed
@galipremsagar

Description

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

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

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Index.difference seems to be treating float vs object / int vs object differently.

Code Sample, a copy-pastable example

import pandas as pd
float_index = pd.Index([1.0, 2, 3])
string_index = pd.Index(['1','2','3'])
integer_index = pd.Index([1, 2, 3])
print(float_index.difference(string_index))
print(string_index.difference(float_index))

print(integer_index.difference(string_index))
print(string_index.difference(integer_index))


Float64Index([], dtype='float64')
Index([], dtype='object')
Int64Index([1, 2, 3], dtype='int64')
Index(['1', '2', '3'], dtype='object')

Problem description

Index.difference should probably not type-cast and compare the elements to honor consistent behavior with other types where they are not being type-casted.

Expected Output

Float64Index([1.0, 2.0, 3.0], dtype='float64')
Index(['1', '2', '3'], dtype='object')
Int64Index([1, 2, 3], dtype='int64')
Index(['1', '2', '3'], dtype='object')

or

All empty Index objects like in case of int vs object.

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None

pandas : 1.0.5
numpy : 1.19.0
pytz : 2020.1
dateutil : 2.8.1
pip : 20.1.1
setuptools : 49.1.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None
None

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIndexRelated to the Index class or subclassesNumeric OperationsArithmetic, Comparison, and Logical operations

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions