Skip to content

DataFrame.rename only validates column arguments #29136

Closed
@WillAyd

Description

@WillAyd

Found this while trying to clean up axis handling in core.generic

This fails as you would hope

>>> df = pd.DataFrame([[1]])
>>> df.rename({0: 1}, columns={0: 2}, axis=1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/williamayd/clones/pandas/pandas/util/_decorators.py", line 235, in wrapper
    return func(*args, **kwargs)
  File "/Users/williamayd/clones/pandas/pandas/core/frame.py", line 4143, in rename
    axes = validate_axis_style_args(self, args, kwargs, "mapper", "rename")
  File "/Users/williamayd/clones/pandas/pandas/util/_validators.py", line 287, in validate_axis_style_args
    raise TypeError(msg)
TypeError: Cannot specify both 'axis' and any of 'index' or 'columns'.

This doesn't

>>> df.rename({0: 1}, index={0: 2})
   0
1  1

And perhaps even more surprising is that you will get a different result depending on whether the first argument is passed by position or keyword

>>> df.rename(mapper={0: 1}, index={0: 2})
   0
2  1

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugDataFrameDataFrame data structure

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions