Skip to content

Commit a8df0b1

Browse files
committed
fix #712 : allowed LArray.reindex(new_axis)
1 parent 5343fc2 commit a8df0b1

File tree

2 files changed

+46
-16
lines changed

2 files changed

+46
-16
lines changed

doc/source/changes/version_0_30.rst.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@ Miscellaneous improvements
288288
Belgian 0 1
289289
FO 2 3
290290

291+
* allowed to pass a single axis or group as ``axes_to_reindex`` argument
292+
of the :py:obj:`LArray.reindex()` method (closes :issue:`712`).
293+
291294

292295
Fixes
293296
^^^^^

larray/core/array.py

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,15 +1339,17 @@ def rename(self, renames=None, to=None, inplace=False, **kwargs):
13391339
return LArray(self.data, axes)
13401340

13411341
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.
13431343
13441344
Place NaN or given `fill_value` in locations having no value previously.
13451345
13461346
Parameters
13471347
----------
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.
13511353
If a list of Axis or an AxisCollection is given, existing axes are reindexed while missing ones are added.
13521354
new_axis : int, str, list/tuple/array of str, Group or Axis, optional
13531355
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
13741376
--------
13751377
>>> arr = ndtest((2, 2))
13761378
>>> arr
1377-
a\\b b0 b1
1379+
a\b b0 b1
13781380
a0 0 1
13791381
a1 2 3
13801382
>>> arr2 = ndtest('a=a1,a2;c=c0;b=b2..b0')
13811383
>>> arr2
1382-
a c\\b b2 b1 b0
1384+
a c\b b2 b1 b0
13831385
a1 c0 0 1 2
13841386
a2 c0 3 4 5
13851387
13861388
Reindex an axis by passing labels (list or string)
13871389
13881390
>>> arr.reindex('b', ['b1', 'b2', 'b0'])
1389-
a\\b b1 b2 b0
1391+
a\b b1 b2 b0
13901392
a0 1.0 nan 0.0
13911393
a1 3.0 nan 2.0
13921394
>>> arr.reindex('b', 'b0..b2', fill_value=-1)
1393-
a\\b b0 b1 b2
1395+
a\b b0 b1 b2
13941396
a0 0 1 -1
13951397
a1 2 3 -1
13961398
>>> arr.reindex(b='b=b0..b2', fill_value=-1)
1397-
a\\b b0 b1 b2
1399+
a\b b0 b1 b2
13981400
a0 0 1 -1
13991401
a1 2 3 -1
14001402
14011403
Reindex using an axis from another array
14021404
14031405
>>> arr.reindex('b', arr2.b, fill_value=-1)
1404-
a\\b b2 b1 b0
1406+
a\b b2 b1 b0
14051407
a0 -1 1 0
14061408
a1 -1 3 2
14071409
14081410
Reindex using a subset of an axis
14091411
14101412
>>> 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
14121429
a0 1 0
14131430
a1 3 2
14141431
14151432
Reindex several axes
14161433
14171434
>>> arr.reindex({'a': arr2.a, 'b': arr2.b}, fill_value=-1)
1418-
a\\b b2 b1 b0
1435+
a\b b2 b1 b0
14191436
a1 -1 3 2
14201437
a2 -1 -1 -1
14211438
>>> arr.reindex({'a': arr2.a, 'b': arr2.b['b1':]}, fill_value=-1)
1422-
a\\b b1 b0
1439+
a\b b1 b0
14231440
a1 3 2
14241441
a2 -1 -1
14251442
>>> arr.reindex(a=arr2.a, b=arr2.b, fill_value=-1)
1426-
a\\b b2 b1 b0
1443+
a\b b2 b1 b0
14271444
a1 -1 3 2
14281445
a2 -1 -1 -1
14291446
14301447
Reindex by passing a collection of axes
14311448
14321449
>>> arr.reindex(arr2.axes, fill_value=-1)
1433-
a b\\c c0
1450+
a b\c c0
14341451
a1 b2 -1
14351452
a1 b1 3
14361453
a1 b0 2
14371454
a2 b2 -1
14381455
a2 b1 -1
14391456
a2 b0 -1
14401457
>>> arr2.reindex(arr.axes, fill_value=-1)
1441-
a c\\b b0 b1
1458+
a c\b b0 b1
14421459
a0 c0 -1 -1
14431460
a1 c0 2 1
14441461
"""
14451462
# 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+
14461472
if new_axis is not None and not isinstance(new_axis, Axis):
14471473
new_axis = Axis(new_axis, self.axes[axes_to_reindex].name)
14481474
elif isinstance(new_axis, Axis):
14491475
new_axis = new_axis.rename(self.axes[axes_to_reindex].name)
1476+
14501477
if isinstance(axes_to_reindex, (list, tuple)) and all([isinstance(axis, Axis) for axis in axes_to_reindex]):
14511478
axes_to_reindex = AxisCollection(axes_to_reindex)
14521479
if isinstance(axes_to_reindex, AxisCollection):

0 commit comments

Comments
 (0)