@@ -1339,15 +1339,17 @@ def rename(self, renames=None, to=None, inplace=False, **kwargs):
1339
1339
return LArray (self .data , axes )
1340
1340
1341
1341
def reindex (self , axes_to_reindex = None , new_axis = None , fill_value = nan , inplace = False , ** kwargs ):
1342
- """Reorder and/or add new labels in axes.
1342
+ r """Reorder and/or add new labels in axes.
1343
1343
1344
1344
Place NaN or given `fill_value` in locations having no value previously.
1345
1345
1346
1346
Parameters
1347
1347
----------
1348
- axes_to_reindex : axis ref or dict {axis ref: axis} or list of tuple (axis ref, axis) \
1349
- or list of Axis or AxisCollection
1350
- Axes to reindex. If a single axis reference is given, the `new_axis` argument must be provided.
1348
+ axes_to_reindex : axis ref or str or Group or Axis or dict {axis ref: axis} or \
1349
+ list of tuple (axis ref, axis) or list of Axis or AxisCollection
1350
+ Axis(es) to reindex. If a single axis reference is given, the `new_axis` argument must be provided.
1351
+ If string or Group or Axis object, the corresponding axis is reindexed if found among existing,
1352
+ otherwise a new axis is added.
1351
1353
If a list of Axis or an AxisCollection is given, existing axes are reindexed while missing ones are added.
1352
1354
new_axis : int, str, list/tuple/array of str, Group or Axis, optional
1353
1355
List of new labels or new axis if `axes_to_reindex` contains a single axis reference.
@@ -1374,79 +1376,104 @@ def reindex(self, axes_to_reindex=None, new_axis=None, fill_value=nan, inplace=F
1374
1376
--------
1375
1377
>>> arr = ndtest((2, 2))
1376
1378
>>> arr
1377
- a\\ b b0 b1
1379
+ a\b b0 b1
1378
1380
a0 0 1
1379
1381
a1 2 3
1380
1382
>>> arr2 = ndtest('a=a1,a2;c=c0;b=b2..b0')
1381
1383
>>> arr2
1382
- a c\\ b b2 b1 b0
1384
+ a c\b b2 b1 b0
1383
1385
a1 c0 0 1 2
1384
1386
a2 c0 3 4 5
1385
1387
1386
1388
Reindex an axis by passing labels (list or string)
1387
1389
1388
1390
>>> arr.reindex('b', ['b1', 'b2', 'b0'])
1389
- a\\ b b1 b2 b0
1391
+ a\b b1 b2 b0
1390
1392
a0 1.0 nan 0.0
1391
1393
a1 3.0 nan 2.0
1392
1394
>>> arr.reindex('b', 'b0..b2', fill_value=-1)
1393
- a\\ b b0 b1 b2
1395
+ a\b b0 b1 b2
1394
1396
a0 0 1 -1
1395
1397
a1 2 3 -1
1396
1398
>>> arr.reindex(b='b=b0..b2', fill_value=-1)
1397
- a\\ b b0 b1 b2
1399
+ a\b b0 b1 b2
1398
1400
a0 0 1 -1
1399
1401
a1 2 3 -1
1400
1402
1401
1403
Reindex using an axis from another array
1402
1404
1403
1405
>>> arr.reindex('b', arr2.b, fill_value=-1)
1404
- a\\ b b2 b1 b0
1406
+ a\b b2 b1 b0
1405
1407
a0 -1 1 0
1406
1408
a1 -1 3 2
1407
1409
1408
1410
Reindex using a subset of an axis
1409
1411
1410
1412
>>> arr.reindex('b', arr2.b['b1':], fill_value=-1)
1411
- a\\ b b1 b0
1413
+ a\b b1 b0
1414
+ a0 1 0
1415
+ a1 3 2
1416
+
1417
+ Reindex by passing an axis or a group
1418
+
1419
+ >>> arr.reindex('b=b2..b0', fill_value=-1)
1420
+ a\b b2 b1 b0
1421
+ a0 -1 1 0
1422
+ a1 -1 3 2
1423
+ >>> arr.reindex(arr2.b, fill_value=-1)
1424
+ a\b b2 b1 b0
1425
+ a0 -1 1 0
1426
+ a1 -1 3 2
1427
+ >>> arr.reindex(arr2.b['b1':], fill_value=-1)
1428
+ a\b b1 b0
1412
1429
a0 1 0
1413
1430
a1 3 2
1414
1431
1415
1432
Reindex several axes
1416
1433
1417
1434
>>> arr.reindex({'a': arr2.a, 'b': arr2.b}, fill_value=-1)
1418
- a\\ b b2 b1 b0
1435
+ a\b b2 b1 b0
1419
1436
a1 -1 3 2
1420
1437
a2 -1 -1 -1
1421
1438
>>> arr.reindex({'a': arr2.a, 'b': arr2.b['b1':]}, fill_value=-1)
1422
- a\\ b b1 b0
1439
+ a\b b1 b0
1423
1440
a1 3 2
1424
1441
a2 -1 -1
1425
1442
>>> arr.reindex(a=arr2.a, b=arr2.b, fill_value=-1)
1426
- a\\ b b2 b1 b0
1443
+ a\b b2 b1 b0
1427
1444
a1 -1 3 2
1428
1445
a2 -1 -1 -1
1429
1446
1430
1447
Reindex by passing a collection of axes
1431
1448
1432
1449
>>> arr.reindex(arr2.axes, fill_value=-1)
1433
- a b\\ c c0
1450
+ a b\c c0
1434
1451
a1 b2 -1
1435
1452
a1 b1 3
1436
1453
a1 b0 2
1437
1454
a2 b2 -1
1438
1455
a2 b1 -1
1439
1456
a2 b0 -1
1440
1457
>>> arr2.reindex(arr.axes, fill_value=-1)
1441
- a c\\ b b0 b1
1458
+ a c\b b0 b1
1442
1459
a0 c0 -1 -1
1443
1460
a1 c0 2 1
1444
1461
"""
1445
1462
# XXX: can't we move this to AxisCollection.replace?
1463
+ if isinstance (axes_to_reindex , basestring ):
1464
+ try :
1465
+ axes_to_reindex = Axis (axes_to_reindex )
1466
+ except :
1467
+ pass
1468
+ if isinstance (axes_to_reindex , (Group , Axis )) and not isinstance (axes_to_reindex , AxisReference ):
1469
+ new_axis = axes_to_reindex if isinstance (axes_to_reindex , Axis ) else Axis (axes_to_reindex )
1470
+ axes_to_reindex = self .axes [new_axis ]
1471
+
1446
1472
if new_axis is not None and not isinstance (new_axis , Axis ):
1447
1473
new_axis = Axis (new_axis , self .axes [axes_to_reindex ].name )
1448
1474
elif isinstance (new_axis , Axis ):
1449
1475
new_axis = new_axis .rename (self .axes [axes_to_reindex ].name )
1476
+
1450
1477
if isinstance (axes_to_reindex , (list , tuple )) and all ([isinstance (axis , Axis ) for axis in axes_to_reindex ]):
1451
1478
axes_to_reindex = AxisCollection (axes_to_reindex )
1452
1479
if isinstance (axes_to_reindex , AxisCollection ):
0 commit comments