Home > Enterprise >  Why instantiate pandas.IndexSlice before use?
Why instantiate pandas.IndexSlice before use?

Time:09-10

Everywhere I see pandas.IndexSlice code (including in the docs) it is instantiated before being used, like this:

idx = pd.IndexSlice
df.loc[idx[:, 'A':'B'], :]  # Sample use of pandas.IndexSlice

Is there a reason to do that, instead of everywhere using it inline like this:

df.loc[pd.IndexSlice[:, 'A':'B'], :]  # Inline use of pandas.IndexSlice

CodePudding user response:

idx = pd.IndexSlice 

Doesn't instantiate it. this is creating an alias, probably because idx is much more readable. but these two are equivalent.

  • Related