Skip to content

Commit 7a6ecf7

Browse files
bkjchoi72jreback
authored andcommitted
DOC: Fixed the doctsring for _set_axis_name (GH 22895) (pandas-dev#22969)
1 parent 9f17a07 commit 7a6ecf7

File tree

1 file changed

+110
-94
lines changed

1 file changed

+110
-94
lines changed

pandas/core/generic.py

+110-94
Original file line numberDiff line numberDiff line change
@@ -1109,35 +1109,41 @@ def rename(self, *args, **kwargs):
11091109
('inplace', False)])
11101110
def rename_axis(self, mapper=None, **kwargs):
11111111
"""
1112-
Alter the name of the index or name of Index object that is the
1113-
columns.
1112+
Set the name of the axis for the index or columns.
11141113
11151114
Parameters
11161115
----------
11171116
mapper : scalar, list-like, optional
11181117
Value to set the axis name attribute.
11191118
index, columns : scalar, list-like, dict-like or function, optional
1120-
dict-like or functions transformations to apply to
1121-
that axis' values.
1119+
A scalar, list-like, dict-like or functions transformations to
1120+
apply to that axis' values.
11221121
11231122
Use either ``mapper`` and ``axis`` to
11241123
specify the axis to target with ``mapper``, or ``index``
11251124
and/or ``columns``.
11261125
11271126
.. versionchanged:: 0.24.0
11281127
1129-
axis : int or string, default 0
1130-
copy : boolean, default True
1128+
axis : {0 or 'index', 1 or 'columns'}, default 0
1129+
The axis to rename.
1130+
copy : bool, default True
11311131
Also copy underlying data.
1132-
inplace : boolean, default False
1132+
inplace : bool, default False
11331133
Modifies the object directly, instead of creating a new Series
11341134
or DataFrame.
11351135
11361136
Returns
11371137
-------
1138-
renamed : Series, DataFrame, or None
1138+
Series, DataFrame, or None
11391139
The same type as the caller or None if `inplace` is True.
11401140
1141+
See Also
1142+
--------
1143+
Series.rename : Alter Series index labels or name.
1144+
DataFrame.rename : Alter DataFrame index labels or name.
1145+
Index.rename : Set new names on index.
1146+
11411147
Notes
11421148
-----
11431149
Prior to version 0.21.0, ``rename_axis`` could also be used to change
@@ -1162,75 +1168,73 @@ def rename_axis(self, mapper=None, **kwargs):
11621168
We *highly* recommend using keyword arguments to clarify your
11631169
intent.
11641170
1165-
See Also
1166-
--------
1167-
Series.rename : Alter Series index labels or name.
1168-
DataFrame.rename : Alter DataFrame index labels or name.
1169-
Index.rename : Set new names on index.
1170-
11711171
Examples
11721172
--------
11731173
**Series**
11741174
1175-
>>> s = pd.Series([1, 2, 3])
1176-
>>> s.rename_axis("foo")
1177-
foo
1178-
0 1
1179-
1 2
1180-
2 3
1181-
dtype: int64
1175+
>>> s = pd.Series(["dog", "cat", "monkey"])
1176+
>>> s
1177+
0 dog
1178+
1 cat
1179+
2 monkey
1180+
dtype: object
1181+
>>> s.rename_axis("animal")
1182+
animal
1183+
0 dog
1184+
1 cat
1185+
2 monkey
1186+
dtype: object
11821187
11831188
**DataFrame**
11841189
1185-
>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
1186-
>>> df.rename_axis("foo")
1187-
A B
1188-
foo
1189-
0 1 4
1190-
1 2 5
1191-
2 3 6
1192-
1193-
>>> df.rename_axis("bar", axis="columns")
1194-
bar A B
1195-
0 1 4
1196-
1 2 5
1197-
2 3 6
1198-
1199-
>>> mi = pd.MultiIndex.from_product([['a', 'b', 'c'], [1, 2]],
1200-
... names=['let','num'])
1201-
>>> df = pd.DataFrame({'x': [i for i in range(len(mi))],
1202-
... 'y' : [i*10 for i in range(len(mi))]},
1203-
... index=mi)
1204-
>>> df.rename_axis(index={'num' : 'n'})
1205-
x y
1206-
let n
1207-
a 1 0 0
1208-
2 1 10
1209-
b 1 2 20
1210-
2 3 30
1211-
c 1 4 40
1212-
2 5 50
1213-
1214-
>>> cdf = df.rename_axis(columns='col')
1215-
>>> cdf
1216-
col x y
1217-
let num
1218-
a 1 0 0
1219-
2 1 10
1220-
b 1 2 20
1221-
2 3 30
1222-
c 1 4 40
1223-
2 5 50
1224-
1225-
>>> cdf.rename_axis(columns=str.upper)
1226-
COL x y
1227-
let num
1228-
a 1 0 0
1229-
2 1 10
1230-
b 1 2 20
1231-
2 3 30
1232-
c 1 4 40
1233-
2 5 50
1190+
>>> df = pd.DataFrame({"num_legs": [4, 4, 2],
1191+
... "num_arms": [0, 0, 2]},
1192+
... ["dog", "cat", "monkey"])
1193+
>>> df
1194+
num_legs num_arms
1195+
dog 4 0
1196+
cat 4 0
1197+
monkey 2 2
1198+
>>> df = df.rename_axis("animal")
1199+
>>> df
1200+
num_legs num_arms
1201+
animal
1202+
dog 4 0
1203+
cat 4 0
1204+
monkey 2 2
1205+
>>> df = df.rename_axis("limbs", axis="columns")
1206+
>>> df
1207+
limbs num_legs num_arms
1208+
animal
1209+
dog 4 0
1210+
cat 4 0
1211+
monkey 2 2
1212+
1213+
**MultiIndex**
1214+
1215+
>>> df.index = pd.MultiIndex.from_product([['mammal'],
1216+
... ['dog', 'cat', 'monkey']],
1217+
... names=['type', 'name'])
1218+
>>> df
1219+
limbs num_legs num_arms
1220+
type name
1221+
mammal dog 4 0
1222+
cat 4 0
1223+
monkey 2 2
1224+
1225+
>>> df.rename_axis(index={'type': 'class'})
1226+
limbs num_legs num_arms
1227+
class name
1228+
mammal dog 4 0
1229+
cat 4 0
1230+
monkey 2 2
1231+
1232+
>>> df.rename_axis(columns=str.upper)
1233+
LIMBS num_legs num_arms
1234+
type name
1235+
mammal dog 4 0
1236+
cat 4 0
1237+
monkey 2 2
12341238
"""
12351239
axes, kwargs = self._construct_axes_from_arguments((), kwargs)
12361240
copy = kwargs.pop('copy', True)
@@ -1285,45 +1289,57 @@ def rename_axis(self, mapper=None, **kwargs):
12851289

12861290
def _set_axis_name(self, name, axis=0, inplace=False):
12871291
"""
1288-
Alter the name or names of the axis.
1292+
Set the name(s) of the axis.
12891293
12901294
Parameters
12911295
----------
12921296
name : str or list of str
1293-
Name for the Index, or list of names for the MultiIndex
1294-
axis : int or str
1295-
0 or 'index' for the index; 1 or 'columns' for the columns
1296-
inplace : bool
1297-
whether to modify `self` directly or return a copy
1297+
Name(s) to set.
1298+
axis : {0 or 'index', 1 or 'columns'}, default 0
1299+
The axis to set the label. The value 0 or 'index' specifies index,
1300+
and the value 1 or 'columns' specifies columns.
1301+
inplace : bool, default False
1302+
If `True`, do operation inplace and return None.
12981303
12991304
.. versionadded:: 0.21.0
13001305
13011306
Returns
13021307
-------
1303-
renamed : same type as caller or None if inplace=True
1308+
Series, DataFrame, or None
1309+
The same type as the caller or `None` if `inplace` is `True`.
13041310
13051311
See Also
13061312
--------
1307-
pandas.DataFrame.rename
1308-
pandas.Series.rename
1309-
pandas.Index.rename
1313+
DataFrame.rename : Alter the axis labels of :class:`DataFrame`.
1314+
Series.rename : Alter the index labels or set the index name
1315+
of :class:`Series`.
1316+
Index.rename : Set the name of :class:`Index` or :class:`MultiIndex`.
13101317
13111318
Examples
13121319
--------
1313-
>>> df._set_axis_name("foo")
1314-
A
1315-
foo
1316-
0 1
1317-
1 2
1318-
2 3
1319-
>>> df.index = pd.MultiIndex.from_product([['A'], ['a', 'b', 'c']])
1320-
>>> df._set_axis_name(["bar", "baz"])
1321-
A
1322-
bar baz
1323-
A a 1
1324-
b 2
1325-
c 3
1326-
"""
1320+
>>> df = pd.DataFrame({"num_legs": [4, 4, 2]},
1321+
... ["dog", "cat", "monkey"])
1322+
>>> df
1323+
num_legs
1324+
dog 4
1325+
cat 4
1326+
monkey 2
1327+
>>> df._set_axis_name("animal")
1328+
num_legs
1329+
animal
1330+
dog 4
1331+
cat 4
1332+
monkey 2
1333+
>>> df.index = pd.MultiIndex.from_product(
1334+
... [["mammal"], ['dog', 'cat', 'monkey']])
1335+
>>> df._set_axis_name(["type", "name"])
1336+
legs
1337+
type name
1338+
mammal dog 4
1339+
cat 4
1340+
monkey 2
1341+
"""
1342+
pd.MultiIndex.from_product([["mammal"], ['dog', 'cat', 'monkey']])
13271343
axis = self._get_axis_number(axis)
13281344
idx = self._get_axis(axis).set_names(name)
13291345

0 commit comments

Comments
 (0)