Skip to content

PERF: regression in CategoricalIndex.get_indexer #42249

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
jorisvandenbossche opened this issue Jun 26, 2021 · 1 comment · Fixed by #42270
Closed

PERF: regression in CategoricalIndex.get_indexer #42249

jorisvandenbossche opened this issue Jun 26, 2021 · 1 comment · Fixed by #42270
Labels
Categorical Categorical Data Type Performance Memory or execution speed performance Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@jorisvandenbossche
Copy link
Member

The fix in #42089 (or caused by the PR that this one was fixing) seems to have caused a large slowdown on the get_indexer benchmarks: https://pandas.pydata.org/speed/pandas/#indexing.CategoricalIndexIndexing.time_get_indexer_list?python=3.8&Cython=0.29.21&p-index='monotonic_incr'&commits=cf5852bf-fce7f9eb

The regression overview (https://pandas.pydata.org/speed/pandas/#regressions?sort=1&dir=desc) lists it as a 1000x slowdown, but that's only because #42042 first improved the performance a lot (which might be a bit suspicious?). Compared to the timing before that, it's only 4-5x slowdown. With the below code, I see locally a ~9x slowdown on master compared to 1.2.5.

import string, itertools
data_unique = pd.CategoricalIndex(
            ["".join(perm) for perm in itertools.permutations(string.printable, 3)]
)
cat_list = ["a", "c"]

%timeit data_unique.get_indexer(cat_list)
52.8 ms ± 5.56 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)  # <-- pandas 1.2.5
417 ms ± 22.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)  # <-- master

I think it has to do with the fact that before we called the Engine.get_indexer on the codes, while now in the base class version we do that with the .categories, which means in this case that both self and target are cast to object dtype and thus use the Engine.get_indexer for object dtype.

Originally posted by @jorisvandenbossche in #42089 (comment)

@jorisvandenbossche jorisvandenbossche added Categorical Categorical Data Type Performance Memory or execution speed performance Regression Functionality that used to work in a prior pandas version labels Jun 26, 2021
@jorisvandenbossche jorisvandenbossche added this to the 1.3 milestone Jun 26, 2021
@jbrockmendel
Copy link
Member

that's only because #42042 first improved the performance a lot (which might be a bit suspicious?)

Before #42042, get_indexer ended up using get_indexer_non_unique, so I don't find the perf improvement there surprising.

which means in this case that both self and target are cast to object dtype

This seems like the likely culprit. I'll see what I can do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Categorical Categorical Data Type Performance Memory or execution speed performance Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants