Why instantiate pandas.IndexSlice before use?

Question:

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
Asked By: feetwet

||

Answers:

idx = pd.IndexSlice 

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

Answered By: juanpa.arrivillaga
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.