Open
Description
I would like to propose that any pandas API that allows specification of a column name also works when specifying an index level name. Today, this works in some places, but not all. Here is a list of places where things work, and where things don't work and could be improved. (The list is most likely incomplete, so additions are welcome). References to existing issues are given when they already exist (and if I knew about them):
Hoping to include this in the roadmap #27478
Things that work:
DataFrame.query()
allows queries to use column names and index level namesDataFrame.merge()
andpd.merge()
allow both column names and index level names to be specified as key fields to use in the merge (but see below)DataFrame.groupby()
allows both column names and index level names to be mixed in the groupby list
Things that could be improved:
- Allow
.loc()
and.getitem()
to specify index level names wherever it allows column names. - Modify
DataFrame.rename()
to allow renaming of index levels with a dict argument. (API: Allow MultiIndex.rename() to accept a dict as an argument #20421) - Allow index names to be specified like columns in
pd.Grouper()
(Grouper should support key from index name (just like groupby supports grouping by index names) #19542) - When merging on a subset of
MultiIndex
levels, preserve the levels not included in the merge. (merge on index vs merge on column - different NaN handling #13371 is somewhat related) - Have
.itertuples()
return a named tuple that includes index names (ENH: Allow index names to be included in itertuples() result #27407) - Allow
.assign
to refer to columns that correspond to level names (although the.loc
and.getitem()
suggestion above might handle this