Skip to content

Commit 1de9e73

Browse files
committed
Update doc-string in .rename
1 parent 2a2d1cf commit 1de9e73

File tree

2 files changed

+67
-30
lines changed

2 files changed

+67
-30
lines changed

pandas/core/indexes/base.py

+66-29
Original file line numberDiff line numberDiff line change
@@ -1278,39 +1278,49 @@ def _set_names(self, values, level=None):
12781278

12791279
def set_names(self, names, level=None, inplace=False):
12801280
"""
1281-
Set new names on index. Defaults to returning new index.
1281+
Set Index or MultiIndex name.
1282+
1283+
Able to set new names partially and by level.
12821284
12831285
Parameters
12841286
----------
1285-
names : str or sequence
1286-
name(s) to set
1287-
level : int, level name, or sequence of int/level names (default None)
1288-
If the index is a MultiIndex (hierarchical), level(s) to set (None
1289-
for all levels). Otherwise level must be None
1290-
inplace : bool
1291-
if True, mutates in place
1287+
names : label or list of labels
1288+
Name(s) to set.
1289+
level : int, str or list of labels, optional
1290+
If the index is a MultiIndex, level(s) to set (None for all
1291+
levels). Otherwise level must be None.
1292+
inplace : boolean, default False
1293+
Modifies the object directly, instead of creating a new Index or
1294+
MultiIndex.
12921295
12931296
Returns
12941297
-------
1295-
new index (of same type and class...etc) [if inplace, returns None]
1298+
Index
1299+
The same type as the caller or None if inplace is True.
1300+
1301+
See Also
1302+
--------
1303+
Index.rename : Able to set new names without level.
12961304
12971305
Examples
12981306
--------
1299-
>>> pd.Index([1, 2, 3, 4]).set_names('foo')
1300-
Int64Index([1, 2, 3, 4], dtype='int64', name='foo')
1301-
>>> pd.Index([1, 2, 3, 4]).set_names(['foo'])
1302-
Int64Index([1, 2, 3, 4], dtype='int64', name='foo')
1303-
>>> idx = pd.MultiIndex.from_tuples([(1, u'one'), (1, u'two'),
1304-
(2, u'one'), (2, u'two')],
1305-
names=['foo', 'bar'])
1306-
>>> idx.set_names(['baz', 'quz'])
1307-
MultiIndex(levels=[[1, 2], [u'one', u'two']],
1307+
>>> idx = pd.Index([1, 2, 3, 4])
1308+
>>> idx
1309+
Int64Index([1, 2, 3, 4], dtype='int64')
1310+
>>> idx.set_names('quarter')
1311+
Int64Index([1, 2, 3, 4], dtype='int64', name='quarter')
1312+
1313+
>>> idx = pd.MultiIndex.from_tuples([(1, 'one'), (1, 'two'),
1314+
... (2, 'one'), (2, 'two')],
1315+
... names=['year', 'company'])
1316+
>>> idx.set_names(['term', 'corporation'])
1317+
MultiIndex(levels=[[1, 2], ['one', 'two']],
13081318
labels=[[0, 0, 1, 1], [0, 1, 0, 1]],
1309-
names=[u'baz', u'quz'])
1310-
>>> idx.set_names('baz', level=0)
1311-
MultiIndex(levels=[[1, 2], [u'one', u'two']],
1319+
names=['term', 'corporation'])
1320+
>>> idx.set_names('association', level=1)
1321+
MultiIndex(levels=[[1, 2], ['one', 'two']],
13121322
labels=[[0, 0, 1, 1], [0, 1, 0, 1]],
1313-
names=[u'baz', u'bar'])
1323+
names=['year', 'association'])
13141324
"""
13151325

13161326
from .multi import MultiIndex
@@ -1319,7 +1329,8 @@ def set_names(self, names, level=None, inplace=False):
13191329

13201330
if level is not None and not is_list_like(level) and is_list_like(
13211331
names):
1322-
raise TypeError("Names must be a string")
1332+
msg = "Names must be a string when one level is provided."
1333+
raise TypeError(msg)
13231334

13241335
if not is_list_like(names) and level is None and self.nlevels > 1:
13251336
raise TypeError("Must pass list-like as `names`.")
@@ -1339,18 +1350,44 @@ def set_names(self, names, level=None, inplace=False):
13391350

13401351
def rename(self, name, inplace=False):
13411352
"""
1342-
Set new names on index. Defaults to returning new index.
1353+
Alter Index or MultiIndex name.
1354+
1355+
Able to set new names without level. Defaults to returning new index.
1356+
Length of names must match number of levels in MultiIndex.
13431357
13441358
Parameters
13451359
----------
1346-
name : str or list
1347-
name to set
1348-
inplace : bool
1349-
if True, mutates in place
1360+
name : label or list of labels
1361+
Name(s) to set.
1362+
inplace : boolean, default False
1363+
Modifies the object directly, instead of creating a new Index or
1364+
MultiIndex.
13501365
13511366
Returns
13521367
-------
1353-
new index (of same type and class...etc) [if inplace, returns None]
1368+
Index
1369+
The same type as the caller or None if inplace is True.
1370+
1371+
See Also
1372+
--------
1373+
Index.set_names : Able to set new names partially and by level.
1374+
1375+
Examples
1376+
--------
1377+
>>> idx = pd.Index(['A', 'C', 'A', 'B'], name='score')
1378+
>>> idx.rename('grade')
1379+
Index(['A', 'C', 'A', 'B'], dtype='object', name='grade')
1380+
1381+
>>> idx = pd.MultiIndex.from_tuples([(1, 'one'), (1, 'two'),
1382+
... (2, 'one'), (2, 'two')],
1383+
... names=['year', 'company'])
1384+
>>> idx.rename(['term', 'corporation'])
1385+
MultiIndex(levels=[[1, 2], ['one', 'two']],
1386+
labels=[[0, 0, 1, 1], [0, 1, 0, 1]],
1387+
names=['term', 'corporation'])
1388+
>>> idx.rename('term')
1389+
Traceback (most recent call last):
1390+
TypeError: Must pass list-like as `names`.
13541391
"""
13551392
return self.set_names([name], inplace=inplace)
13561393

pandas/tests/indexes/multi/test_get_set.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ def test_set_levels_labels_names_bad_input(idx):
360360
with tm.assert_raises_regex(ValueError, 'Length of names'):
361361
idx.set_names(names[0], level=[0, 1])
362362

363-
with tm.assert_raises_regex(TypeError, 'string'):
363+
with tm.assert_raises_regex(TypeError, 'Names must be a'):
364364
idx.set_names(names, level=0)
365365

366366

0 commit comments

Comments
 (0)