Skip to content

Commit 7947c6e

Browse files
committed
fix #671 : implemented 'integerstring[labels]' and 'integerstring.i[indices]'
1 parent f4af203 commit 7947c6e

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

doc/source/changes/version_0_30.rst.inc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,26 @@ Miscellaneous improvements
153153

154154
Closes :issue:`669`.
155155

156+
* allowed to specify an axis by its postion when selecting a subset of an array using the string notation:
157+
158+
>>> pop_mouv = ndtest('geo_from=BE,FR,UK;geo_to=BE,FR,UK')
159+
>>> pop_mouv
160+
geo_from\geo_to BE FR UK
161+
BE 0 1 2
162+
FR 3 4 5
163+
UK 6 7 8
164+
>>> pop_mouv['0[BE, UK]'] # equivalent to pop_mouv[pop_mouv.geo_from['BE,UK']]
165+
geo_from\geo_to BE FR UK
166+
BE 0 1 2
167+
UK 6 7 8
168+
>>> pop_mouv['1.i[0, 2]'] # equivalent to pop_mouv[pop_mouv.geo_to.i[0, 2]]
169+
geo_from\geo_to BE UK
170+
BE 0 2
171+
FR 3 5
172+
UK 6 8
173+
174+
Closes :issue:`671`.
175+
156176

157177
Fixes
158178
-----

larray/core/axis.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2318,6 +2318,14 @@ def _translate_axis_key(self, axis_key, bool_passthrough=True):
23182318
"""
23192319
from .array import LArray
23202320

2321+
# Need to convert string keys to groups otherwise command like
2322+
# >>> ndtest((5, 5)).drop('1[a0]')
2323+
# will work although it shouldn't
2324+
if isinstance(axis_key, basestring):
2325+
key = _to_key(axis_key)
2326+
if isinstance(key, Group):
2327+
axis_key = key
2328+
23212329
if isinstance(axis_key, ExprNode):
23222330
raise Exception('yada')
23232331
axis_key = axis_key.evaluate(self)

larray/core/group.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from larray.core.abstractbases import ABCAxis, ABCAxisReference, ABCLArray
1414
from larray.util.oset import *
15-
from larray.util.misc import (basestring, PY2, unique, find_closing_chr, _parse_bound, _seq_summary,
15+
from larray.util.misc import (basestring, PY2, unique, find_closing_chr, _parse_bound, _seq_summary, _isintstring,
1616
renamed_to, LHDFStore)
1717

1818
_deprecated = ['PGroup']
@@ -439,7 +439,7 @@ def _to_ticks(s, parse_single_int=False):
439439
return np.asarray(ticks)
440440

441441

442-
_axis_name_pattern = re.compile('\s*(([A-Za-z]\w*)(\.i)?\s*\[)?(.*)')
442+
_axis_name_pattern = re.compile('\s*(([A-Za-z0-9]\w*)(\.i)?\s*\[)?(.*)')
443443

444444

445445
def _seq_str_to_seq(s, stack_depth=1, parse_single_int=False):
@@ -530,6 +530,10 @@ def _to_key(v, stack_depth=1, parse_single_int=False):
530530
LGroup(['a', 'b', 'c']) >> 'abc'
531531
>>> _to_key('a:c >> abc')
532532
LGroup(slice('a', 'c', None)) >> 'abc'
533+
>>> _to_key('0[a0]')
534+
0['a0']
535+
>>> _to_key('0.i[a0]')
536+
0.i['a0']
533537
534538
# evaluated variables do not work on Python 2, probably because the stackdepth is different
535539
# >>> ext = [1, 2, 3]
@@ -564,6 +568,8 @@ def _to_key(v, stack_depth=1, parse_single_int=False):
564568
key, name = key[:name_pos].strip(), key[name_pos + 2:].strip()
565569
if axis is not None:
566570
axis = axis.strip()
571+
if _isintstring(axis):
572+
axis = int(axis)
567573
axis_bracket_open = m.end(1) - 1
568574
# check that the string parentheses are correctly balanced
569575
_ = find_closing_chr(v, axis_bracket_open)

larray/tests/test_array.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,21 @@ def test_getitem_igroup_on_int_axis():
658658
assert arr[a.i[1]] == 1
659659

660660

661+
def test_getitem_integer_string_axes():
662+
arr = ndtest((5, 5))
663+
a, b = arr.axes
664+
665+
assert_array_equal(arr['0[a0, a2]'], arr[a['a0', 'a2']])
666+
assert_array_equal(arr['0[a0:a2]'], arr[a['a0:a2']])
667+
with pytest.raises(ValueError):
668+
arr['1[a0, a2]']
669+
670+
assert_array_equal(arr['0.i[0, 2]'], arr[a.i[0, 2]])
671+
assert_array_equal(arr['0.i[0:2]'], arr[a.i[0:2]])
672+
with pytest.raises(ValueError):
673+
arr['3.i[0, 2]']
674+
675+
661676
def test_getitem_int_larray_lgroup_key():
662677
# e axis go from 0 to 3
663678
arr = ndtest("c=0,1; d=0,1; e=0..3")

0 commit comments

Comments
 (0)