Skip to content

Convert DataFrame.rename to keyword only; simplify axis validation #29140

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

Merged
merged 18 commits into from
Jan 9, 2020
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v1.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,9 @@ New repr for :class:`~pandas.arrays.IntervalArray`
1 1

*pandas 1.0.0*

.. ipython:: python
:okexcept:

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

Expand Down
10 changes: 9 additions & 1 deletion pandas/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
from typing import (
IO,
TYPE_CHECKING,
Any,
AnyStr,
Callable,
Collection,
Dict,
Hashable,
List,
Mapping,
Optional,
TypeVar,
Union,
Expand Down Expand Up @@ -56,10 +60,14 @@
FrameOrSeries = TypeVar("FrameOrSeries", bound="NDFrame")

Axis = Union[str, int]
Level = Union[str, int]
Label = Optional[Hashable]
Level = Union[Label, int]
Ordered = Optional[bool]
JSONSerializable = Union[PythonScalar, List, Dict]
Axes = Collection

# For functions like rename that convert one label to another
Renamer = Union[Mapping[Hashable, Any], Callable[[Hashable], Hashable]]

# to maintain type information across generic functions and parametrization
T = TypeVar("T")
18 changes: 5 additions & 13 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@
IO,
TYPE_CHECKING,
Any,
Callable,
FrozenSet,
Hashable,
Iterable,
List,
Mapping,
Optional,
Sequence,
Set,
Expand All @@ -40,7 +38,7 @@
from pandas._config import get_option

from pandas._libs import algos as libalgos, lib
from pandas._typing import Axes, Axis, Dtype, FilePathOrBuffer, Level
from pandas._typing import Axes, Axis, Dtype, FilePathOrBuffer, Level, Renamer
from pandas.compat import PY37
from pandas.compat._optional import import_optional_dependency
from pandas.compat.numpy import function as nv
Expand Down Expand Up @@ -3990,22 +3988,16 @@ def drop(
)
def rename(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change here was to be explicit about what is accepted

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to being explicit. The only reason we had *args, **kwargs in the first place was to support the ambiguous case.

self,
mapper: Optional[
Union[Mapping[Hashable, Hashable], Callable[[Hashable], Hashable]]
] = None,
mapper: Optional[Renamer] = None,
*,
index: Optional[
Union[Mapping[Hashable, Hashable], Callable[[Hashable], Hashable]]
] = None,
columns: Optional[
Union[Mapping[Hashable, Hashable], Callable[[Hashable], Hashable]]
] = None,
index: Optional[Renamer] = None,
columns: Optional[Renamer] = None,
axis: Optional[Axis] = None,
copy: bool = True,
inplace: bool = False,
level: Optional[Level] = None,
errors: str = "ignore",
) -> "DataFrame":
) -> Optional["DataFrame"]:

"""
Alter axes labels.
Expand Down
16 changes: 6 additions & 10 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
FrameOrSeries,
JSONSerializable,
Level,
Renamer,
)
from pandas.compat import set_function_name
from pandas.compat._optional import import_optional_dependency
Expand Down Expand Up @@ -930,16 +931,10 @@ def swaplevel(self: FrameOrSeries, i=-2, j=-1, axis=0) -> FrameOrSeries:

def rename(
self,
mapper: Optional[
Union[Mapping[Hashable, Any], Callable[[Hashable], Hashable]]
] = None,
mapper: Optional[Renamer] = None,
*,
index: Optional[
Union[Mapping[Hashable, Any], Callable[[Hashable], Hashable]]
] = None,
columns: Optional[
Union[Mapping[Hashable, Any], Callable[[Hashable], Hashable]]
] = None,
index: Optional[Renamer] = None,
columns: Optional[Renamer] = None,
axis: Optional[Axis] = None,
copy: bool = True,
inplace: bool = False,
Expand Down Expand Up @@ -1072,7 +1067,7 @@ def rename(
)
else:
# use the mapper argument
if axis in {1, "columns"}:
if axis and self._get_axis_number(axis) == 1:
columns = mapper
else:
index = mapper
Expand Down Expand Up @@ -1108,6 +1103,7 @@ def rename(

if inplace:
self._update_inplace(result._data)
return None
else:
return result.__finalize__(self)

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3124,7 +3124,7 @@ def argsort(self, axis=0, kind="quicksort", order=None):

Parameters
----------
axis : int
axis : {0 or "index"}
Has no effect but is accepted for compatibility with numpy.
kind : {'mergesort', 'quicksort', 'heapsort'}, default 'quicksort'
Choice of sorting algorithm. See np.sort for more
Expand Down