Skip to content

Commit cb59c68

Browse files
committed
fix #634 : allowed omitting axis for LArray.set_labels
1 parent 3357571 commit cb59c68

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

doc/source/changes/version_0_30.rst.inc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,18 @@ Miscellaneous improvements
276276
the current and future version of larray. To get benefit of the speedup of reading arrays dumped
277277
with older versions of larray, please read and re-dump them. Closes :issue:`563`.
278278

279+
* allowed to not specifiy the axes in :py:obj:`LArray.set_labels()` (closes :issue:`634`):
280+
281+
>>> a = ndtest('nat=BE,FO;sex=M,F')
282+
>>> a
283+
nat\sex M F
284+
BE 0 1
285+
FO 2 3
286+
>>> a.set_labels({'M': 'Men', 'BE': 'Belgian'})
287+
nat\sex Men F
288+
Belgian 0 1
289+
FO 2 3
290+
279291

280292
Fixes
281293
^^^^^

larray/core/array.py

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6592,28 +6592,6 @@ def __array__(self, dtype=None):
65926592

65936593
__array_priority__ = 100
65946594

6595-
# XXX: implement guess axis?
6596-
"""
6597-
# guessing each axis
6598-
>>> a.set_labels({'M': 'Men', 'BE': 'Belgian'})
6599-
nat\\sex Men Women
6600-
BE 0 1
6601-
FO 2 3
6602-
6603-
# we have to choose which one to support because it is probably not a good idea to simultaneously support the
6604-
# following syntax (even though we *could* support both if we split values on , before we determine if the key is
6605-
# an axis or a label by looking if the value is a list or a single string.
6606-
>>> a.set_labels({'sex': 'Men,Women', 'BE': 'Belgian'})
6607-
nat\\sex Men Women
6608-
BE 0 1
6609-
FO 2 3
6610-
# this is shorter but I do not like it because string are both quoted and not quoted and you cannot have int
6611-
# labels
6612-
>>> a.set_labels(M='Men', BE='Belgian')
6613-
nat\\sex Men Women
6614-
BE 0 1
6615-
FO 2 3
6616-
"""
66176595
def set_labels(self, axis=None, labels=None, inplace=False, **kwargs):
66186596
r"""Replaces the labels of an axis of array.
66196597
@@ -6622,6 +6600,8 @@ def set_labels(self, axis=None, labels=None, inplace=False, **kwargs):
66226600
axis : string or Axis or dict
66236601
Axis for which we want to replace labels, or mapping {axis: changes} where changes can either be the
66246602
complete list of labels, a mapping {old_label: new_label} or a function to transform labels.
6603+
If there is no ambiguity (two or more axes have the same labels), `axis` can be a direct mapping
6604+
{old_label: new_label}.
66256605
labels : int, str, iterable or mapping or function, optional
66266606
Integer or list of values usable as the collection of labels for an Axis. If this is mapping, it must be
66276607
{old_label: new_label}. If it is a function, it must be a function accepting a single argument (a
@@ -6690,6 +6670,14 @@ def set_labels(self, axis=None, labels=None, inplace=False, **kwargs):
66906670
66916671
>>> a.set_labels({'sex': {'M': 'Men'}, 'nat': {'BE': 'Belgian'}})
66926672
nat\sex Men F
6673+
Belgian 0 1
6674+
FO 2 3
6675+
6676+
when there is no ambiguity (two or more axes have the same labels), it is possible to give a mapping
6677+
between old and new labels
6678+
6679+
>>> a.set_labels({'M': 'Men', 'BE': 'Belgian'})
6680+
nat\sex Men F
66936681
Belgian 0 1
66946682
FO 2 3
66956683
"""
@@ -6706,14 +6694,18 @@ def set_labels(self, axis=None, labels=None, inplace=False, **kwargs):
67066694
# new_axes = [self.axes[old_axis].replace(axis_changes) for old_axis, axis_changes in changes.items()]
67076695
new_axes = []
67086696
for old_axis, axis_changes in changes.items():
6709-
real_axis = self.axes[old_axis]
6697+
try:
6698+
real_axis = self.axes[old_axis]
6699+
except KeyError:
6700+
axis_changes = {old_axis: axis_changes}
6701+
real_axis = self._guess_axis(old_axis).axis
67106702
if isinstance(axis_changes, dict):
67116703
new_axis = real_axis.replace(axis_changes)
67126704
elif callable(axis_changes):
67136705
new_axis = real_axis.apply(axis_changes)
67146706
else:
67156707
new_axis = Axis(axis_changes, real_axis.name)
6716-
new_axes.append((old_axis, new_axis))
6708+
new_axes.append((real_axis, new_axis))
67176709
axes = self.axes.replace(new_axes)
67186710

67196711
if inplace:

0 commit comments

Comments
 (0)