Skip to content

Commit c0b09dc

Browse files
committed
fix #479 : renamed Axis.translate to Axis.index
1 parent 80f572a commit c0b09dc

File tree

5 files changed

+23
-15
lines changed

5 files changed

+23
-15
lines changed

doc/source/api.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Searching
4242
.. autosummary::
4343
:toctree: _generated/
4444

45-
Axis.translate
45+
Axis.index
4646
Axis.containing
4747
Axis.startingwith
4848
Axis.endingwith

doc/source/changes/version_0_27.rst.inc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
Backward incompatible changes
1+
Syntax changes
2+
--------------
3+
4+
* renamed `Axis.translate` to `Axis.index` (closes :issue:`479`).
5+
6+
7+
Backward incompatible changes
28
-----------------------------
39

410
* labels are checked during array subset assignement (closes :issue:`269`):

larray/core/array.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,7 +1754,7 @@ def _translate_axis_key_chunk(self, axis_key, bool_passthrough=True):
17541754
if isinstance(axis_key, LGroup) and axis_key.axis is not None:
17551755
real_axis = self.axes[axis_key.axis]
17561756
try:
1757-
axis_pos_key = real_axis.translate(axis_key, bool_passthrough)
1757+
axis_pos_key = real_axis.index(axis_key, bool_passthrough)
17581758
except KeyError:
17591759
raise ValueError("%r is not a valid label for any axis" % axis_key)
17601760
return real_axis.i[axis_pos_key]
@@ -1768,7 +1768,7 @@ def _translate_axis_key_chunk(self, axis_key, bool_passthrough=True):
17681768
# TODO: use axis_key dtype to only check compatible axes
17691769
for axis in self.axes:
17701770
try:
1771-
axis_pos_key = axis.translate(axis_key, bool_passthrough)
1771+
axis_pos_key = axis.index(axis_key, bool_passthrough)
17721772
valid_axes.append(axis)
17731773
except KeyError:
17741774
continue
@@ -1856,7 +1856,7 @@ def _guess_axis(self, axis_key):
18561856
valid_axes = []
18571857
for axis in self.axes:
18581858
try:
1859-
axis.translate(axis_key)
1859+
axis.index(axis_key)
18601860
valid_axes.append(axis)
18611861
except KeyError:
18621862
continue
@@ -1954,7 +1954,7 @@ def _translated_key(self, key, bool_stuff=False):
19541954
for axis in self.axes]
19551955

19561956
# IGroup -> raw positional
1957-
return tuple(axis.translate(axis_key, bool_passthrough=not bool_stuff)
1957+
return tuple(axis.index(axis_key, bool_passthrough=not bool_stuff)
19581958
for axis, axis_key in zip(self.axes, key))
19591959

19601960
# TODO: we only need axes length => move this to AxisCollection

larray/core/axis.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ def _is_key_type_compatible(self, key):
675675
# object kind can match anything
676676
return key_kind == label_kind or key_kind == 'O' or label_kind == 'O' or py2_str_match
677677

678-
def translate(self, key, bool_passthrough=True):
678+
def index(self, key, bool_passthrough=True):
679679
"""
680680
Translates a label key to its numerical index counterpart.
681681
@@ -698,9 +698,9 @@ def translate(self, key, bool_passthrough=True):
698698
Examples
699699
--------
700700
>>> people = Axis(['John Doe', 'Bruce Wayne', 'Bruce Willis', 'Waldo', 'Arthur Dent', 'Harvey Dent'], 'people')
701-
>>> people.translate('Waldo')
701+
>>> people.index('Waldo')
702702
3
703-
>>> people.translate(people.matching('Bruce'))
703+
>>> people.index(people.matching('Bruce'))
704704
array([1, 2])
705705
"""
706706
mapping = self._mapping
@@ -784,7 +784,7 @@ def translate(self, key, bool_passthrough=True):
784784
return array_lookup2(key, self._sorted_keys, self._sorted_values)
785785
elif isinstance(key, ABCLArray):
786786
from .array import LArray
787-
return LArray(self.translate(key.data), key.axes)
787+
return LArray(self.index(key.data), key.axes)
788788
else:
789789
# the first mapping[key] above will cover most cases.
790790
# This code path is only used if the key was given in "non normalized form"
@@ -796,6 +796,8 @@ def translate(self, key, bool_passthrough=True):
796796
# print("diff dtype", )
797797
raise KeyError(key)
798798

799+
translate = renamed_to(index, 'translate')
800+
799801
# FIXME: remove id
800802
@property
801803
def id(self):
@@ -946,7 +948,7 @@ def replace(self, old, new=None):
946948
assert len(old) == len(new)
947949
# using object dtype because new labels length can be larger than the fixed str length in the self.labels array
948950
labels = self.labels.astype(object)
949-
indices = self.translate(old)
951+
indices = self.index(old)
950952
labels[indices] = new
951953
return Axis(labels, self.name)
952954

@@ -1993,7 +1995,7 @@ def translate_full_key(self, key):
19931995
(slice(None, None, None), 1, 2)
19941996
"""
19951997
assert len(key) == len(self)
1996-
return tuple(axis.translate(axis_key) for axis_key, axis in zip(key, self))
1998+
return tuple(axis.index(axis_key) for axis_key, axis in zip(key, self))
19971999

19982000
@property
19992001
def labels(self):
@@ -2515,7 +2517,7 @@ def __init__(self, name):
25152517
self._labels = None
25162518
self._iswildcard = False
25172519

2518-
def translate(self, key):
2520+
def index(self, key):
25192521
raise NotImplementedError("an AxisReference (X.) cannot translate labels")
25202522

25212523
def __repr__(self):

larray/tests/test_axis.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ def test_translate(self):
106106
# an axis with labels having the object dtype
107107
a = Axis(np.array(["a0", "a1"], dtype=object), 'a')
108108

109-
self.assertEqual(a.translate('a1'), 1)
110-
self.assertEqual(a.translate('a1 >> A1'), 1)
109+
self.assertEqual(a.index('a1'), 1)
110+
self.assertEqual(a.index('a1 >> A1'), 1)
111111

112112
def test_getitem_lgroup_keys(self):
113113
def group_equal(g1, g2):

0 commit comments

Comments
 (0)