ENH: Allow to set axis name in pd.concat #56553
Labels
Enhancement
Needs Discussion
Requires discussion from core team before further action
Reshaping
Concat, Merge/Join, Stack/Unstack, Explode
Feature Type
Adding new functionality to pandas
Changing existing functionality in pandas
Removing existing functionality in pandas
Problem Description
pd.concat()
has the parameternames
which works fine when using thekeys
parameter together with thenames
parameter. For example:Output:
The name of index level 1 is now
"class"
(same as it was before).However, using the
names
parameter alone doesn't change anything, no error appears and no changes are made to the name of the index. This is a bit in accordance to the docs, which say to thenames
parameter:Feature Description
I find it more satisfying if
names
could be used in a single index too, just to set the name of the resulting single index (i.e. rename the index axis).Expected output:
My suggestion is that using the
names
parameter without thekeys
parameter would set the name of the resulting axis for the single index (if a single index is returned). On a side note, I don't know what is usally used as argument in such a casenames=["class"]
ornames="class"
, I think you know more than me.Alternative Solutions
The following example works right now but only because the name of the index in the first object of
pd.concat([df, df.agg(["sum"])
is already"class"
:pd.concat([df, df.agg(["sum"]).rename_axis("class")])
Otherwise one would need to use:
pd.concat([df, df.agg(["sum"])]).rename_axis("class")
The proposed solution seems to be more readable:
pd.concat([df, df.agg(["sum"])], names=["class"])
Additional Context
If this would be implemented, the docs for the parameter
names
would need to be adapted, e.g. to something like:Names for the levels in the resulting single index or Multiindex.
The text was updated successfully, but these errors were encountered: