diff --git a/pandas/core/generic.py b/pandas/core/generic.py index e92059c552b65..8fa86e80e1a44 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -90,10 +90,7 @@ SettingWithCopyError, SettingWithCopyWarning, ) -from pandas.util._decorators import ( - doc, - rewrite_axis_style_signature, -) +from pandas.util._decorators import doc from pandas.util._exceptions import find_stack_level from pandas.util._validators import ( validate_ascending, @@ -509,38 +506,6 @@ def _construct_axes_dict(self, axes: Sequence[Axis] | None = None, **kwargs): d.update(kwargs) # type: ignore[arg-type] return d - @final - @classmethod - def _construct_axes_from_arguments( - cls, args, kwargs, require_all: bool_t = False, sentinel=None - ): - """ - Construct and returns axes if supplied in args/kwargs. - - If require_all, raise if all axis arguments are not supplied - return a tuple of (axes, kwargs). - - sentinel specifies the default parameter when an axis is not - supplied; useful to distinguish when a user explicitly passes None - in scenarios where None has special meaning. - """ - # construct the args - args = list(args) - for a in cls._AXIS_ORDERS: - - # look for a argument by position - if a not in kwargs: - try: - kwargs[a] = args.pop(0) - except IndexError as err: - if require_all: - raise TypeError( - "not enough/duplicate arguments specified!" - ) from err - - axes = {a: kwargs.pop(a, sentinel) for a in cls._AXIS_ORDERS} - return axes, kwargs - @final @classmethod def _get_axis_number(cls, axis: Axis) -> AxisInt: @@ -1077,8 +1042,11 @@ def rename_axis( self: NDFrameT, mapper: IndexLabel | lib.NoDefault = ..., *, + index=..., + columns=..., + axis: Axis = ..., + copy: bool_t | None = ..., inplace: Literal[False] = ..., - **kwargs, ) -> NDFrameT: ... @@ -1087,8 +1055,11 @@ def rename_axis( self, mapper: IndexLabel | lib.NoDefault = ..., *, + index=..., + columns=..., + axis: Axis = ..., + copy: bool_t | None = ..., inplace: Literal[True], - **kwargs, ) -> None: ... @@ -1097,18 +1068,23 @@ def rename_axis( self: NDFrameT, mapper: IndexLabel | lib.NoDefault = ..., *, + index=..., + columns=..., + axis: Axis = ..., + copy: bool_t | None = ..., inplace: bool_t = ..., - **kwargs, ) -> NDFrameT | None: ... - @rewrite_axis_style_signature("mapper", [("copy", True)]) def rename_axis( self: NDFrameT, mapper: IndexLabel | lib.NoDefault = lib.no_default, *, + index=lib.no_default, + columns=lib.no_default, + axis: Axis = 0, + copy: bool_t | None = None, inplace: bool_t = False, - **kwargs, ) -> NDFrameT | None: """ Set the name of the axis for the index or columns. @@ -1129,7 +1105,7 @@ def rename_axis( and/or ``columns``. axis : {0 or 'index', 1 or 'columns'}, default 0 The axis to rename. For `Series` this parameter is unused and defaults to 0. - copy : bool, default True + copy : bool, default None Also copy underlying data. inplace : bool, default False Modifies the object directly, instead of creating a new Series @@ -1233,23 +1209,11 @@ class name cat 4 0 monkey 2 2 """ - kwargs["inplace"] = inplace - axes, kwargs = self._construct_axes_from_arguments( - (), kwargs, sentinel=lib.no_default - ) - copy: bool_t | None = kwargs.pop("copy", None) + axes = {"index": index, "columns": columns} - inplace = kwargs.pop("inplace", False) - axis = kwargs.pop("axis", 0) if axis is not None: axis = self._get_axis_number(axis) - if kwargs: - raise TypeError( - "rename_axis() got an unexpected keyword " - f'argument "{list(kwargs.keys())[0]}"' - ) - inplace = validate_bool_kwarg(inplace, "inplace") if mapper is not lib.no_default: @@ -4530,7 +4494,9 @@ def drop( axis_name = self._get_axis_name(axis) axes = {axis_name: labels} elif index is not None or columns is not None: - axes, _ = self._construct_axes_from_arguments((index, columns), {}) + axes = {"index": index} + if self.ndim == 2: + axes["columns"] = columns else: raise ValueError( "Need to specify at least one of 'labels', 'index' or 'columns'" diff --git a/pandas/core/series.py b/pandas/core/series.py index 63420309f33fc..ed23edc55805d 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -4903,6 +4903,24 @@ def reindex( # type: ignore[override] tolerance=tolerance, ) + @doc(NDFrame.rename_axis) + def rename_axis( # type: ignore[override] + self: Series, + mapper: IndexLabel | lib.NoDefault = lib.no_default, + *, + index=lib.no_default, + axis: Axis = 0, + copy: bool = True, + inplace: bool = False, + ) -> Series | None: + return super().rename_axis( + mapper=mapper, + index=index, + axis=axis, + copy=copy, + inplace=inplace, + ) + @overload def drop( self, diff --git a/pandas/util/_decorators.py b/pandas/util/_decorators.py index 4a382f7984971..968da7cf60105 100644 --- a/pandas/util/_decorators.py +++ b/pandas/util/_decorators.py @@ -337,36 +337,6 @@ def wrapper(*args, **kwargs): return decorate -def rewrite_axis_style_signature( - name: str, extra_params: list[tuple[str, Any]] -) -> Callable[[F], F]: - def decorate(func: F) -> F: - @wraps(func) - def wrapper(*args, **kwargs) -> Callable[..., Any]: - return func(*args, **kwargs) - - kind = inspect.Parameter.POSITIONAL_OR_KEYWORD - params = [ - inspect.Parameter("self", kind), - inspect.Parameter(name, kind, default=None), - inspect.Parameter("index", kind, default=None), - inspect.Parameter("columns", kind, default=None), - inspect.Parameter("axis", kind, default=None), - ] - - for pname, default in extra_params: - params.append(inspect.Parameter(pname, kind, default=default)) - - sig = inspect.Signature(params) - - # https://github.com/python/typing/issues/598 - # error: "F" has no attribute "__signature__" - func.__signature__ = sig # type: ignore[attr-defined] - return cast(F, wrapper) - - return decorate - - def doc(*docstrings: None | str | Callable, **params) -> Callable[[F], F]: """ A decorator take docstring templates, concatenate them and perform string @@ -531,6 +501,5 @@ def indent(text: str | None, indents: int = 1) -> str: "deprecate_nonkeyword_arguments", "doc", "future_version_msg", - "rewrite_axis_style_signature", "Substitution", ]