Skip to content

Multiindex Equals Not Commutative #46026

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

Closed
3 tasks done
hliatrussellinvestments opened this issue Feb 16, 2022 · 1 comment · Fixed by #46047
Closed
3 tasks done

Multiindex Equals Not Commutative #46026

hliatrussellinvestments opened this issue Feb 16, 2022 · 1 comment · Fixed by #46047
Labels
Bug ExtensionArray Extending pandas with custom dtypes or arrays. MultiIndex
Milestone

Comments

@hliatrussellinvestments

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

df1 = pd.DataFrame({
    'a': [0, 0, 1, 1],
    'b': [0, 1, 0, 1],
    'c': [0, 0, 0, 0],
    'v': [1, 2, 3, 4],
})
df1['a'] = df1['a'].astype('Int32')


df2 = pd.DataFrame({
    'a': [0, 0, 1, 1],
    'b': [0, 1, 0, 1],
    'v': [1, 2, 3, 4],
})
df2.set_index(['a', 'b'], inplace=True)

df1_agg = df1.groupby(['a', 'b']).agg({'v': 'max'})

print(df1_agg.index.equals(df2.index) == df2.index.equals(df1_agg.index))

Issue Description

For any 2 indexes, idx_1, idx_2, we should get the same result for idx_1.equals(idx_2) and idx_2.equals(idx_1). Currently they are not the same.

Expected Behavior

print(df1_agg.index.equals(df2.index) == df2.index.equals(df1_agg.index)) should print out True.

Installed Versions

INSTALLED VERSIONS

commit : bb1f651
python : 3.8.6.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19041
machine : AMD64
processor : Intel64 Family 6 Model 158 Stepping 13, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252
pandas : 1.4.0
numpy : 1.19.0
pytz : 2021.3
dateutil : 2.8.1
pip : 21.3.1
setuptools : 49.2.1
Cython : None
pytest : 5.4.1
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
fsspec : 2021.10.0
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.5.4
sqlalchemy : 1.4.31
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None

@hliatrussellinvestments hliatrussellinvestments added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 16, 2022
@lukemanley
Copy link
Member

lukemanley commented Feb 16, 2022

Looks like this block is causing the behavior:

if not isinstance(self_values, np.ndarray):
# i.e. ExtensionArray
if not self_values.equals(other_values):
return False
else:
if not array_equivalent(self_values, other_values):
return False

Here is an example:

import numpy as np
import pandas as pd
from pandas.core.dtypes.missing import array_equivalent

a = pd.array([1, 2, 3], dtype="Int32")
b = np.array([1, 2, 3])

a.equals(b)               # False
array_equivalent(a, b)    # True

@lukemanley lukemanley added ExtensionArray Extending pandas with custom dtypes or arrays. Index Related to the Index class or subclasses MultiIndex and removed Needs Triage Issue that has not been reviewed by a pandas team member Index Related to the Index class or subclasses labels Feb 16, 2022
@jreback jreback added this to the 1.5 milestone Feb 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug ExtensionArray Extending pandas with custom dtypes or arrays. MultiIndex
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants