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

PERF: MultiIndex.equals for equal length indexes #56990

Merged
merged 3 commits into from
Jan 23, 2024

Conversation

lukemanley
Copy link
Member

@lukemanley lukemanley commented Jan 21, 2024

Avoids densifying the levels of the multiindex to check for equality.

from asv_bench.benchmarks.multiindex_object import Equals

b = Equals()
b.setup()
%timeit b.time_equals_deepcopy()

# 56.4 ms ± 1.03 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)   -> main
# 3.77 ms ± 58.4 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)  -> PR

@lukemanley lukemanley added Performance Memory or execution speed performance MultiIndex labels Jan 21, 2024
@lukemanley lukemanley added this to the 3.0 milestone Jan 21, 2024
@lukemanley lukemanley changed the title PERF: MultiIndex.equals PERF: MultiIndex.equals for equal length indexes Jan 21, 2024
)
if not np.array_equal(self_codes, new_codes):
return False
if not self_level[:0].equals(other_level[:0]):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we just use is_dtype_equal?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The equality rules for numpy dtypes are not as strict as is_dtype_equal:

In [1]: import pandas as pd

In [2]: import numpy as np

In [3]: idx = pd.Index([1])

In [4]: idx.astype("int64").equals(idx.astype("int32"))
Out[4]: True

In [5]: pd.core.dtypes.common.is_dtype_equal(np.int64, np.int32)
Out[5]: False

@mroeschke mroeschke merged commit 7281475 into pandas-dev:main Jan 23, 2024
@mroeschke
Copy link
Member

Thanks @lukemanley

pmhatre1 pushed a commit to pmhatre1/pandas-pmhatre1 that referenced this pull request May 7, 2024
* avoid densifying level values in MultiIndex.equals

* whatsnew
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
MultiIndex Performance Memory or execution speed performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants