Skip to content

Commit 6f60ef7

Browse files
committed
FEAT: allow ; in __getitem__ and __setitem__ (closes larray-project#904)
so that we can target multiple axes in a single string. eg. arr['a1;b1'] or arr['b1..b3;a2,a1']
1 parent f5bfd59 commit 6f60ef7

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

doc/source/changes/version_0_34.rst.inc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ Backward incompatible changes
1818
New features
1919
^^^^^^^^^^^^
2020

21+
* allow ";" in __getitem__ and __setitem__ strings, making it possible to specify keys for several axes in a single
22+
string (closes :issue:`904`). For example:
23+
24+
>>> arr = ndtest((2, 3))
25+
>>> arr
26+
a\b b0 b1 b2
27+
a0 0 1 2
28+
a1 3 4 5
29+
>>> arr['a1;b2']
30+
5
31+
2132
* added a feature (see the :ref:`miscellaneous section <misc>` for details). It works on :ref:`api-axis` and
2233
:ref:`api-group` objects.
2334

larray/core/axis.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2811,6 +2811,12 @@ def _key_to_axis_indices_dict(self, key):
28112811
"""
28122812
from .array import Array
28132813

2814+
if isinstance(key, str) and ';' in key:
2815+
# FIXME: it would be more logical to use _to_keys(key) instead but this breaks
2816+
# the "target an aggregate with its key" feature (test_getitem_on_group_agg).
2817+
# It might be a good thing though as it costs so much for so little use.
2818+
# key = _to_keys(key)
2819+
key = tuple(key.split(';'))
28142820
if isinstance(key, dict):
28152821
# key axes could be strings or axis references and we want real axes
28162822
key = tuple(self[axis][axis_key] for axis, axis_key in key.items())

larray/tests/test_array.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,14 @@ def test_getitem_guess_axis(array):
651651
res = arr['b[l1]']
652652
assert_array_equal(res, arr.data[:, 0])
653653

654+
# several axes in same string (guess axis)
655+
res = arr['l0;l2']
656+
assert res == 1
657+
658+
# several axes in same string (with axis information)
659+
res = arr['a[l1];b[l2]']
660+
assert res == 3
661+
654662

655663
def test_getitem_positional_group(array):
656664
raw = array.data

0 commit comments

Comments
 (0)