@@ -546,13 +546,16 @@ def swaplevel(self, i, j, axis=0):
546546 _shared_docs ['rename' ] = """
547547 Alter axes input function or functions. Function / dict values must be
548548 unique (1-to-1). Labels not contained in a dict / Series will be left
549- as-is.
549+ as-is. Alternatively, change ``Series.name`` with a scalar
550+ value (Series only).
550551
551552 Parameters
552553 ----------
553- %(axes)s : dict-like or function, optional
554- Transformation to apply to that axis values
555-
554+ %(axes)s : scalar, list-like, dict-like or function, optional
555+ Scalar or list-like will alter the ``Series.name`` attribute,
556+ and raise on DataFrame or Panel.
557+ dict-like or functions are transformations to apply to
558+ that axis' values
556559 copy : boolean, default True
557560 Also copy underlying data
558561 inplace : boolean, default False
@@ -562,6 +565,43 @@ def swaplevel(self, i, j, axis=0):
562565 Returns
563566 -------
564567 renamed : %(klass)s (new object)
568+
569+ See Also
570+ --------
571+ pandas.NDFrame.rename_axis
572+
573+ Examples
574+ --------
575+ >>> s = pd.Series([1, 2, 3])
576+ >>> s
577+ 0 1
578+ 1 2
579+ 2 3
580+ dtype: int64
581+ >>> s.rename("my_name") # scalar, changes Series.name
582+ 0 1
583+ 1 2
584+ 2 3
585+ Name: my_name, dtype: int64
586+ >>> s.rename(lambda x: x ** 2) # function, changes labels
587+ 0 1
588+ 1 2
589+ 4 3
590+ dtype: int64
591+ >>> s.rename({1: 3, 2: 5}) # mapping, changes labels
592+ 0 1
593+ 3 2
594+ 5 3
595+ dtype: int64
596+ >>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
597+ >>> df.rename(2)
598+ ...
599+ TypeError: 'int' object is not callable
600+ >>> df.rename(index=str, columns={"A": "a", "B": "c"})
601+ a c
602+ 0 1 4
603+ 1 2 5
604+ 2 3 6
565605 """
566606
567607 @Appender (_shared_docs ['rename' ] % dict (axes = 'axes keywords for this'
@@ -617,12 +657,15 @@ def f(x):
617657 def rename_axis (self , mapper , axis = 0 , copy = True , inplace = False ):
618658 """
619659 Alter index and / or columns using input function or functions.
660+ A scaler or list-like for ``mapper`` will alter the ``Index.name``
661+ or ``MultiIndex.names`` attribute.
662+ A function or dict for ``mapper`` will alter the labels.
620663 Function / dict values must be unique (1-to-1). Labels not contained in
621664 a dict / Series will be left as-is.
622665
623666 Parameters
624667 ----------
625- mapper : dict-like or function, optional
668+ mapper : scalar, list-like, dict-like or function, optional
626669 axis : int or string, default 0
627670 copy : boolean, default True
628671 Also copy underlying data
@@ -631,11 +674,88 @@ def rename_axis(self, mapper, axis=0, copy=True, inplace=False):
631674 Returns
632675 -------
633676 renamed : type of caller
677+
678+ See Also
679+ --------
680+ pandas.NDFrame.rename
681+ pandas.Index.rename
682+
683+ Examples
684+ --------
685+ >>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
686+ >>> df.rename_axis("foo") # scalar, alters df.index.name
687+ A B
688+ foo
689+ 0 1 4
690+ 1 2 5
691+ 2 3 6
692+ >>> df.rename_axis(lambda x: 2 * x) # function: alters labels
693+ A B
694+ 0 1 4
695+ 2 2 5
696+ 4 3 6
697+ >>> df.rename_axis({"A": "ehh", "C": "see"}, axis="columns") # mapping
698+ ehh B
699+ 0 1 4
700+ 1 2 5
701+ 2 3 6
702+ """
703+ is_scalar_or_list = (
704+ (not com .is_sequence (mapper ) and not callable (mapper )) or
705+ (com .is_list_like (mapper ) and not com .is_dict_like (mapper ))
706+ )
707+
708+ if is_scalar_or_list :
709+ return self ._set_axis_name (mapper , axis = axis )
710+ else :
711+ axis = self ._get_axis_name (axis )
712+ d = {'copy' : copy , 'inplace' : inplace }
713+ d [axis ] = mapper
714+ return self .rename (** d )
715+
716+ def _set_axis_name (self , name , axis = 0 ):
634717 """
635- axis = self ._get_axis_name (axis )
636- d = {'copy' : copy , 'inplace' : inplace }
637- d [axis ] = mapper
638- return self .rename (** d )
718+ Alter the name or names of the axis, returning self.
719+
720+ Parameters
721+ ----------
722+ name : str or list of str
723+ Name for the Index, or list of names for the MultiIndex
724+ axis : int or str
725+ 0 or 'index' for the index; 1 or 'columns' for the columns
726+
727+ Returns
728+ -------
729+ renamed : type of caller
730+
731+ See Also
732+ --------
733+ pandas.DataFrame.rename
734+ pandas.Series.rename
735+ pandas.Index.rename
736+
737+ Examples
738+ --------
739+ >>> df._set_axis_name("foo")
740+ A
741+ foo
742+ 0 1
743+ 1 2
744+ 2 3
745+ >>> df.index = pd.MultiIndex.from_product([['A'], ['a', 'b', 'c']])
746+ >>> df._set_axis_name(["bar", "baz"])
747+ A
748+ bar baz
749+ A a 1
750+ b 2
751+ c 3
752+ """
753+ axis = self ._get_axis_number (axis )
754+ idx = self ._get_axis (axis ).set_names (name )
755+
756+ renamed = self .copy (deep = True )
757+ renamed .set_axis (axis , idx )
758+ return renamed
639759
640760 # ----------------------------------------------------------------------
641761 # Comparisons
0 commit comments