113
113
by : str or list of str
114
114
Name or list of names which refer to the axis items.""" ,
115
115
versionadded_to_excel = '' ,
116
- optional_mapper = """mapper : dict-like or function
117
- Applied to the axis specified by `axis`""" ,
118
116
optional_labels = """labels : array-like, optional
119
117
New labels / index to conform the axis specified by 'axis' to.""" ,
120
118
optional_axis = """axis : int or str, optional
@@ -2776,7 +2774,7 @@ def reindexer(value):
2776
2774
2777
2775
def _validate_axis_style_args (self , arg , arg_name , index , columns ,
2778
2776
axis , method_name ):
2779
- if axis != 0 :
2777
+ if axis is not None :
2780
2778
# Using "axis" style, along with a positional arg
2781
2779
# Both index and columns should be None then
2782
2780
axis = self ._get_axis_name (axis )
@@ -2811,6 +2809,7 @@ def _validate_axis_style_args(self, arg, arg_name, index, columns,
2811
2809
warnings .warn (msg , stacklevel = 3 )
2812
2810
index , columns = arg , index
2813
2811
elif index is None :
2812
+ # This is for the default axis, like reindex([0, 1])
2814
2813
index = arg
2815
2814
return index , columns
2816
2815
@@ -2940,7 +2939,7 @@ def align(self, other, join='outer', axis=None, level=None, copy=True,
2940
2939
broadcast_axis = broadcast_axis )
2941
2940
2942
2941
@Appender (_shared_docs ['reindex' ] % _shared_doc_kwargs )
2943
- def reindex (self , labels = None , index = None , columns = None , axis = 0 ,
2942
+ def reindex (self , labels = None , index = None , columns = None , axis = None ,
2944
2943
** kwargs ):
2945
2944
index , columns = self ._validate_axis_style_args (labels , 'labels' ,
2946
2945
index , columns ,
@@ -2956,9 +2955,81 @@ def reindex_axis(self, labels, axis=0, method=None, level=None, copy=True,
2956
2955
method = method , level = level , copy = copy ,
2957
2956
limit = limit , fill_value = fill_value )
2958
2957
2959
- @Appender (_shared_docs ['rename' ] % _shared_doc_kwargs )
2960
- def rename (self , mapper = None , index = None , columns = None , axis = 0 ,
2958
+ def rename (self , mapper = None , index = None , columns = None , axis = None ,
2961
2959
** kwargs ):
2960
+ """Alter axes labels.
2961
+
2962
+ Function / dict values must be unique (1-to-1). Labels not contained in
2963
+ a dict / Series will be left as-is. Extra labels listed don't throw an
2964
+ error.
2965
+
2966
+ See the :ref:`user guide <basics.rename>` for more.
2967
+
2968
+ Parameters
2969
+ ----------
2970
+ mapper : dict-like or function, optional
2971
+ Applied to the axis specified by ``axis``
2972
+ index, columns : scalar, list-like, dict-like or function, optional
2973
+ dict-like or functions transformations to apply to
2974
+ that axis' values
2975
+ axis : int or str, optional
2976
+ Axis to target with ``mapper``. Can be either the axis name
2977
+ ('index', 'columns') or number (0, 1). The default is 'index'.
2978
+ copy : boolean, default True
2979
+ Also copy underlying data
2980
+ inplace : boolean, default False
2981
+ Whether to return a new %(klass)s. If True then value of copy is
2982
+ ignored.
2983
+ level : int or level name, default None
2984
+ In case of a MultiIndex, only rename labels in the specified
2985
+ level.
2986
+
2987
+ Returns
2988
+ -------
2989
+ renamed : DataFrame
2990
+
2991
+ See Also
2992
+ --------
2993
+ pandas.NDFrame.rename_axis
2994
+
2995
+ Examples
2996
+ --------
2997
+
2998
+ ``DataFrame.rename`` supports two calling conventions
2999
+
3000
+ * ``(index=index_mapper, columns=columns_mapper, ...)
3001
+ * ``(mapper, axis={'index', 'columns'}, ...)
3002
+
3003
+ We *highly* recommend using keyword arguments to clarify your
3004
+ intent.
3005
+
3006
+ >>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
3007
+ >>> df.rename(index=str, columns={"A": "a", "B": "c"})
3008
+ a c
3009
+ 0 1 4
3010
+ 1 2 5
3011
+ 2 3 6
3012
+
3013
+ >>> df.rename(index=str, columns={"A": "a", "C": "c"})
3014
+ a B
3015
+ 0 1 4
3016
+ 1 2 5
3017
+ 2 3 6
3018
+
3019
+ Using axis-style parameters
3020
+
3021
+ >>> df.rename(str.lower, axis='columns')
3022
+ a b
3023
+ 0 1 4
3024
+ 1 2 5
3025
+ 2 3 6
3026
+
3027
+ >>> df.rename({1: 2, 2: 4}, axis='index')
3028
+ A B
3029
+ 0 1 4
3030
+ 2 2 5
3031
+ 4 3 6
3032
+ """
2962
3033
index , columns = self ._validate_axis_style_args (mapper , 'mapper' ,
2963
3034
index , columns ,
2964
3035
axis , 'rename' )
0 commit comments