Skip to content

Commit 0b2a11c

Browse files
authored
fix #531 : added support for using groups in reindex
* AxisCollection.setitem also accept a group as value argument.
1 parent f4981f0 commit 0b2a11c

4 files changed

Lines changed: 74 additions & 42 deletions

File tree

‎doc/source/changes/version_0_28.rst.inc‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,19 @@
1010
Miscellaneous improvements
1111
--------------------------
1212

13+
* added possibility to call the method `reindex` with a group (closes :issue:`531`):
14+
15+
>>> arr = ndtest((2, 2))
16+
>>> arr
17+
a\b b0 b1
18+
a0 0 1
19+
a1 2 3
20+
>>> b = Axis("b=b2..b0")
21+
>>> arr.reindex('b', b['b1':])
22+
a\b b1 b0
23+
a0 1 0
24+
a1 3 2
25+
1326
* added possibility to call the methods `diff` and `growth_rate` with a group (closes :issue:`532`):
1427

1528
>>> data = [[2, 4, 5, 4, 6], [4, 6, 3, 6, 9]]

‎larray/core/array.py‎

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,9 +1312,8 @@ def reindex(self, axes_to_reindex=None, new_axis=None, fill_value=np.nan, inplac
13121312
axes_to_reindex : axis ref or dict {axis ref: axis} or list of tuple (axis ref, axis) \
13131313
or list of Axis or AxisCollection
13141314
Axes to reindex. If a single axis reference is given, the `new_axis` argument must be provided.
1315-
If a list of Axis or an AxisCollection is given, all axes will be reindexed by the new ones.
1316-
In that case, the number of new axes must match the number of the old ones.
1317-
new_axis : int, str, list/tuple/array of str or Axis, optional
1315+
If a list of Axis or an AxisCollection is given, existing axes are reindexed while missing ones are added.
1316+
new_axis : int, str, list/tuple/array of str, Group or Axis, optional
13181317
List of new labels or new axis if `axes_to_replace` contains a single axis reference.
13191318
fill_value : scalar or LArray, optional
13201319
Value used to fill cells corresponding to label combinations which were not present before reindexing.
@@ -1342,58 +1341,70 @@ def reindex(self, axes_to_reindex=None, new_axis=None, fill_value=np.nan, inplac
13421341
a\\b b0 b1
13431342
a0 0 1
13441343
a1 2 3
1344+
>>> arr2 = ndrange('a=a1,a2;c=c0;b=b2..b0')
1345+
>>> arr2
1346+
a c\\b b2 b1 b0
1347+
a1 c0 0 1 2
1348+
a2 c0 3 4 5
13451349
1346-
Reindex one axis
1350+
Reindex an axis by passing labels (list or string)
13471351
1348-
>>> arr.reindex(X.b, ['b1', 'b2', 'b0'], fill_value=-1)
1349-
a\\b b1 b2 b0
1350-
a0 1 -1 0
1351-
a1 3 -1 2
1352-
>>> arr.reindex(X.b, 'b0..b2', fill_value=-1)
1352+
>>> arr.reindex('b', ['b1', 'b2', 'b0'])
1353+
a\\b b1 b2 b0
1354+
a0 1.0 nan 0.0
1355+
a1 3.0 nan 2.0
1356+
>>> arr.reindex('b', 'b0..b2', fill_value=-1)
13531357
a\\b b0 b1 b2
13541358
a0 0 1 -1
13551359
a1 2 3 -1
13561360
1357-
Reindex several axes
1361+
Reindex using an axis from another array
13581362
1359-
>>> a = Axis(['a1', 'a2', 'a0'], 'a')
1360-
>>> b = Axis(['b2', 'b1', 'b0'], 'b')
1361-
>>> arr.reindex({'a': a, 'b': b}, fill_value=-1)
1363+
>>> arr.reindex('b', arr2.b, fill_value=-1)
13621364
a\\b b2 b1 b0
1363-
a1 -1 3 2
1364-
a2 -1 -1 -1
13651365
a0 -1 1 0
1366-
>>> arr.reindex({X.a: a, X.b: b})
1367-
a\\b b2 b1 b0
1368-
a1 nan 3.0 2.0
1369-
a2 nan nan nan
1370-
a0 nan 1.0 0.0
1366+
a1 -1 3 2
13711367
1372-
Reindex using axes from another array
1368+
Reindex using a subset of an axis
13731369
1374-
>>> arr2 = ndrange('a=a0,a1;c=c0..c0;b=b0..b2')
1375-
>>> arr2
1376-
a c\\b b0 b1 b2
1377-
a0 c0 0 1 2
1378-
a1 c0 3 4 5
1379-
>>> arr.reindex(arr2.axes)
1380-
a b\\c c0
1381-
a0 b0 0.0
1382-
a0 b1 1.0
1383-
a0 b2 nan
1384-
a1 b0 2.0
1385-
a1 b1 3.0
1386-
a1 b2 nan
1387-
>>> arr2.reindex(arr.axes)
1388-
a c\\b b0 b1
1389-
a0 c0 0.0 1.0
1390-
a1 c0 3.0 4.0
1370+
>>> arr.reindex('b', arr2.b['b1':], fill_value=-1)
1371+
a\\b b1 b0
1372+
a0 1 0
1373+
a1 3 2
1374+
1375+
Reindex several axes
1376+
1377+
>>> arr.reindex({'a': arr2.a, 'b': arr2.b}, fill_value=-1)
1378+
a\\b b2 b1 b0
1379+
a1 -1 3 2
1380+
a2 -1 -1 -1
1381+
>>> arr.reindex({'a': arr2.a, 'b': arr2.b['b1':]}, fill_value=-1)
1382+
a\\b b1 b0
1383+
a1 3 2
1384+
a2 -1 -1
1385+
1386+
Reindex by passing a collection of axes
1387+
1388+
>>> arr.reindex(arr2.axes, fill_value=-1)
1389+
a b\\c c0
1390+
a1 b2 -1
1391+
a1 b1 3
1392+
a1 b0 2
1393+
a2 b2 -1
1394+
a2 b1 -1
1395+
a2 b0 -1
1396+
>>> arr2.reindex(arr.axes, fill_value=-1)
1397+
a c\\b b0 b1
1398+
a0 c0 -1 -1
1399+
a1 c0 2 1
13911400
"""
13921401
# XXX: can't we move this to AxisCollection.replace?
1393-
if isinstance(new_axis, (int, basestring, list, tuple, np.ndarray)):
1402+
if new_axis is not None and not isinstance(new_axis, Axis):
13941403
new_axis = Axis(new_axis, self.axes[axes_to_reindex].name)
1395-
if isinstance(new_axis, Axis):
1404+
elif isinstance(new_axis, Axis):
13961405
new_axis = new_axis.rename(self.axes[axes_to_reindex].name)
1406+
if isinstance(axes_to_reindex, (list, tuple)) and all([isinstance(axis, Axis) for axis in axes_to_reindex]):
1407+
axes_to_reindex = AxisCollection(axes_to_reindex)
13971408
if isinstance(axes_to_reindex, AxisCollection):
13981409
assert new_axis is None
13991410
# add extra axes if needed

‎larray/core/axis.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,9 +1411,8 @@ def slice_bound(bound):
14111411
for k, v in zip(key, value):
14121412
self[k] = v
14131413
else:
1414-
if isinstance(value, (int, basestring, list, tuple)):
1414+
if not isinstance(value, Axis):
14151415
value = Axis(value)
1416-
assert isinstance(value, Axis)
14171416
idx = self.index(key)
14181417
step = 1 if idx >= 0 else -1
14191418
self[idx:idx + step:step] = [value]

‎larray/tests/test_array.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2370,6 +2370,15 @@ def test_reindex(self):
23702370
v1 4 5 6 7
23712371
v3 -1 -1 -1 -1"""))
23722372

2373+
# passing a list of Axis
2374+
arr = ndtest((2, 2))
2375+
res = arr.reindex([Axis("a=a0,a1"), Axis("c=c0"), Axis("b=b1,b2")], fill_value=-1)
2376+
assert_array_equal(res, from_string(""" a b\\c c0
2377+
a0 b1 1
2378+
a0 b2 -1
2379+
a1 b1 3
2380+
a1 b2 -1"""))
2381+
23732382
def test_append(self):
23742383
la = self.small
23752384
sex, lipro = la.axes

0 commit comments

Comments
 (0)