diff --git a/pandas/core/generic.py b/pandas/core/generic.py index a893b2ba1a189..e276cc9abfdf2 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -913,20 +913,25 @@ def f(x): rename.__doc__ = _shared_docs['rename'] def rename_axis(self, mapper, axis=0, copy=True, inplace=False): - """Alter the name of the index or columns. + """ + Alter the name of the index or columns. Parameters ---------- mapper : scalar, list-like, optional - Value to set the axis name attribute. - axis : int or string, default 0 + Value to set as the axis name attribute. + axis : {0 or 'index', 1 or 'columns'}, default 0 + The index or the name of the axis. copy : boolean, default True - Also copy underlying data + Also copy underlying data. inplace : boolean, default False + Modifies the object directly, instead of creating a new Series + or DataFrame. Returns ------- - renamed : type of caller or None if inplace=True + renamed : Series, DataFrame, or None + The same type as the caller or None if `inplace` is True. Notes ----- @@ -937,11 +942,23 @@ def rename_axis(self, mapper, axis=0, copy=True, inplace=False): See Also -------- - pandas.Series.rename, pandas.DataFrame.rename - pandas.Index.rename + pandas.Series.rename : Alter Series index labels or name + pandas.DataFrame.rename : Alter DataFrame index labels or name + pandas.Index.rename : Set new names on index Examples -------- + **Series** + + >>> s = pd.Series([1, 2, 3]) + >>> s.rename_axis("foo") + foo + 0 1 + 1 2 + 2 3 + dtype: int64 + + **DataFrame** >>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}) >>> df.rename_axis("foo") @@ -956,7 +973,6 @@ def rename_axis(self, mapper, axis=0, copy=True, inplace=False): 0 1 4 1 2 5 2 3 6 - """ inplace = validate_bool_kwarg(inplace, 'inplace') non_mapper = is_scalar(mapper) or (is_list_like(mapper) and not