Skip to content

ENH: Add index=/columns= kwargs to df.set_axis() #45467

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

Already on GitHub? Sign in to your account

Open
wany-oh opened this issue Jan 19, 2022 · 4 comments
Open

ENH: Add index=/columns= kwargs to df.set_axis() #45467

wany-oh opened this issue Jan 19, 2022 · 4 comments
Assignees
Labels
API - Consistency Internal Consistency of API/Behavior Enhancement Indexing Related to indexing on series/frames, not to indexes themselves

Comments

@wany-oh
Copy link
Contributor

wany-oh commented Jan 19, 2022

I think it is strange that .set_axis() can't accept index=/columns= kwargs, even though similar methods .rename(), .rename_axis(), .drop() and etc. can.

Personally, I think it would be more readable than specifying it with 0 or 1.
One trivial advantage is that it allows us to change rows and columns at once.

expect:

>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
>>> df.set_axis(index=['a', 'b', 'c'])
   A  B
a  1  4
b  2  5
c  3  6

>>> df.set_axis(columns=['I', 'II'])
   I  II
0  1   4
1  2   5
2  3   6

>>> df.set_axis(index=['a', 'b', 'c'], columns=['I', 'II'])
   I  II
a  1   4
b  2   5
c  3   6
@wany-oh wany-oh added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 19, 2022
@rhshadrach
Copy link
Member

+1 for the API consistency here.

@rhshadrach rhshadrach added API - Consistency Internal Consistency of API/Behavior Indexing Related to indexing on series/frames, not to indexes themselves and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 23, 2022
@rhshadrach rhshadrach added this to the Contributions Welcome milestone Jan 23, 2022
@leonarduschen
Copy link
Contributor

take

@leonarduschen
Copy link
Contributor

How should this work with Series?

Consideration:

  • DataFrame.rename signature:
    def rename(
        self,
        mapper: Renamer | None = None,
        *,
        index: Renamer | None = None,
        columns: Renamer | None = None,
        axis: Axis | None = None,
        copy: bool = True,
        inplace: bool = False,
        level: Level | None = None,
        errors: IgnoreRaise = "ignore",
    ) -> DataFrame | None:
  • Series.rename signature:
    def rename(
        self,
        index: Renamer | Hashable | None = None,
        *,
        axis: Axis | None = None,
        copy: bool = True,
        inplace: bool = False,
        level: Level | None = None,
        errors: IgnoreRaise = "ignore",
    ) -> Series | None:

Series.rename accepts all signature of DataFrame.rename except for mapper and columns (documentation is wrong, already reported at #46889)

Currently, we have DataFrame.set_axis and Series.set_axis current signature:

def set_axis(self, labels, axis: Axis = 0, inplace: bool = False):

We'll add index and columns to DataFrame.set_axis:

def set_axis(self, labels, index, columns, axis: Axis = 0, inplace: bool = False):

I think we shouldn't make any change to Series.set_axis. It still wouldn't be 100% consistent with .rename since Series.rename accepts index instead of mapper, but I don't think there's any way around it.

@rhshadrach
Copy link
Member

Agreed on not changing Series.set_axis (similar to how Series.rename doesn't have columns). I may be misunderstanding, but I don't think there should be consistency with mapper for Series.rename; supplying a mapper to Series.rename doesn't make sense.

@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API - Consistency Internal Consistency of API/Behavior Enhancement Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

No branches or pull requests

4 participants