@@ -1278,39 +1278,49 @@ def _set_names(self, values, level=None):
1278
1278
1279
1279
def set_names (self , names , level = None , inplace = False ):
1280
1280
"""
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.
1282
1284
1283
1285
Parameters
1284
1286
----------
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.
1292
1295
1293
1296
Returns
1294
1297
-------
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.
1296
1304
1297
1305
Examples
1298
1306
--------
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']],
1308
1318
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']],
1312
1322
labels=[[0, 0, 1, 1], [0, 1, 0, 1]],
1313
- names=[u'baz ', u'bar '])
1323
+ names=['year ', 'association '])
1314
1324
"""
1315
1325
1316
1326
from .multi import MultiIndex
@@ -1319,7 +1329,8 @@ def set_names(self, names, level=None, inplace=False):
1319
1329
1320
1330
if level is not None and not is_list_like (level ) and is_list_like (
1321
1331
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 )
1323
1334
1324
1335
if not is_list_like (names ) and level is None and self .nlevels > 1 :
1325
1336
raise TypeError ("Must pass list-like as `names`." )
@@ -1339,18 +1350,44 @@ def set_names(self, names, level=None, inplace=False):
1339
1350
1340
1351
def rename (self , name , inplace = False ):
1341
1352
"""
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.
1343
1357
1344
1358
Parameters
1345
1359
----------
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.
1350
1365
1351
1366
Returns
1352
1367
-------
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`.
1354
1391
"""
1355
1392
return self .set_names ([name ], inplace = inplace )
1356
1393
0 commit comments