Skip to content

BUG: Unable to compare unordered categories #51543

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

Open
3 tasks done
ParthivNaresh opened this issue Feb 21, 2023 · 3 comments
Open
3 tasks done

BUG: Unable to compare unordered categories #51543

ParthivNaresh opened this issue Feb 21, 2023 · 3 comments
Assignees
Labels
Bug Categorical Categorical Data Type

Comments

@ParthivNaresh
Copy link

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
ser_1 = pd.Series([1, 2, 3, "b"], dtype="category")
ser_2 = pd.Series([1, 3, 3, "b"], dtype="category")

ser_2 = ser_2.cat.add_categories(2)

print(f"Ser_1 categories: {ser_1.cat.categories}")
print(f"Ser_2 categories: {ser_2.cat.categories}")

ser_1.eq(ser_2)
===============================================
Ser_1 categories: Index([1, 2, 3, 'b'], dtype='object')
Ser_2 categories: Index([1, 3, 'b', 2], dtype='object')
File "/.../site-packages/pandas/core/arrays/categorical.py", line 153, in func
    raise TypeError(msg)
TypeError: Categoricals can only be compared if 'categories' are the same.

Issue Description

I believe this PR may have oversimplified categorical comparisons and prevented unordered categories from being compared.

Expected Behavior

I would expect that comparing two collections of categories would not raise an error if the sets of those two categories are equivalent regardless of the order.

Installed Versions

IINSTALLED VERSIONS

commit : 2e218d1
python : 3.8.16.final.0
python-bits : 64
OS : Darwin
OS-release : 21.6.0
Version : Darwin Kernel Version 21.6.0: Mon Aug 22 20:17:10 PDT 2022; root:xnu-8020.140.49~2/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.5.3
numpy : 1.23.5
pytz : 2022.7.1
dateutil : 2.8.2
setuptools : 67.1.0
pip : 23.0
Cython : None
pytest : 7.0.1
hypothesis : None
sphinx : 5.1.1
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.4.0
pandas_datareader: None
bs4 : 4.11.2
bottleneck : None
brotli : None
fastparquet : None
fsspec : 2023.1.0
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 11.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.10.0
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

@ParthivNaresh ParthivNaresh added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 21, 2023
@ParthivNaresh ParthivNaresh changed the title BUG: Unable to compare unordered Categoricals with the same unordered categories BUG: Unable to compare unordered categories Feb 21, 2023
@AlexKirko AlexKirko added Categorical Categorical Data Type and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 22, 2023
@AlexKirko
Copy link
Member

Confirmed by reproducing. Since categories are unordered by default, I agree that there shouldn't be an error and we should just get False here.

@ParthivNaresh Would you be willing to take on making a PR to fix this?

@ParthivNaresh
Copy link
Author

@AlexKirko For sure I'm on it!

@ParthivNaresh
Copy link
Author

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Categorical Categorical Data Type
Projects
None yet
2 participants