PERF: reindex with MultiIndex by lukemanley · Pull Request #46235 · pandas-dev/pandas (original) (raw)

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 andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation5 Commits3 Checks0 Files changed

Conversation

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters

[ Show hidden characters]({{ revealButtonHref }})

lukemanley

Avoid unnecessary (and potentially expensive) call to MultiIndex._values. This only matters when MultiIndex._values has not been cached. Existing asv has been updated to test cached and non-cached cases.

import numpy as np
import pandas as pd

mi = pd.MultiIndex.from_arrays(
  [np.arange(10**6)] * 2
)
mi2 = pd.MultiIndex.from_arrays(
  [np.arange(10**7)] * 2
)

df = pd.DataFrame({"A": 1.0}, index=mi)

%timeit x = df.reindex(mi2.copy())
1.84 s ± 106 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)   <- main
685 ms ± 22.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)  <- PR

jbrockmendel

@@ -3922,14 +3922,15 @@ def _get_indexer(
elif method == "nearest":
indexer = self._get_nearest_indexer(target, limit, tolerance)
else:
tgt_values = target._get_engine_target()

Choose a reason for hiding this comment

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

nice!

@jreback

@twoertwein

I think this PR made pyright unhappy, but I honestly don't see how this PR affected _mixins.py:

pandas/core/arrays/_mixins.py:185:26 - warning: Instance methods should take a "self" parameter (reportSelfClsParameterName)

If the solution is to ignore this warning, you can add
# pyright: reportSelfClsParameterName = false
at the top of the file.

edit:
or rename cls to self:

def _from_factorized(cls, values, original):

@jreback

@twoertwein

i think was a prior PR

Yes, you are right: #46214 If no one beats me, I will create a PR to rename cls to self

yehoshuadimarsky pushed a commit to yehoshuadimarsky/pandas that referenced this pull request

Jul 13, 2022

@lukemanley @yehoshuadimarsky