Skip to content

Commit 3fc14a3

Browse files
committed
fix #536: LArray.set_labels can now take functions to transform labels
1 parent 33bb146 commit 3fc14a3

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

doc/source/changes/version_0_30.rst.inc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Syntax changes
1+
Syntax changes
22
--------------
33

44
* new syntax
@@ -139,6 +139,18 @@ Miscellaneous improvements
139139
>>> people.matching(pattern='[AB]*')
140140
people['Bruce Wayne', 'Bruce Willis', 'Arthur Dent']
141141

142+
* :py:obj:`LArray.set_labels()` can now take functions to transform axes labels (closes :issue:`536`).
143+
144+
>>> arr = ndtest((2, 2))
145+
>>> arr
146+
a\b b0 b1
147+
a0 0 1
148+
a1 2 3
149+
>>> arr.set_labels('a', str.upper)
150+
a\b b0 b1
151+
A0 0 1
152+
A1 2 3
153+
142154
* added option ``exact`` to ``join`` argument of :py:obj:`Axis.align()` and :py:obj:`LArray.align()` methods.
143155
Instead of aligning, passing ``join='exact'`` to the ``align`` method will raise an error when axes are not equal.
144156
Closes :issue:`338`.

larray/core/array.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6570,16 +6570,17 @@ def __array__(self, dtype=None):
65706570
FO 2 3
65716571
"""
65726572
def set_labels(self, axis=None, labels=None, inplace=False, **kwargs):
6573-
"""Replaces the labels of an axis of array.
6573+
r"""Replaces the labels of an axis of array.
65746574
65756575
Parameters
65766576
----------
65776577
axis : string or Axis or dict
65786578
Axis for which we want to replace labels, or mapping {axis: changes} where changes can either be the
6579-
complete list of labels or a mapping {old_label: new_label}.
6580-
labels : int, str, iterable or mapping, optional
6579+
complete list of labels, a mapping {old_label: new_label} or a function to transform labels.
6580+
labels : int, str, iterable or mapping or function, optional
65816581
Integer or list of values usable as the collection of labels for an Axis. If this is mapping, it must be
6582-
{old_label: new_label}. This argument must not be used if axis is a mapping.
6582+
{old_label: new_label}. If it is a function, it must be a function accepting a single argument (a
6583+
label) and returning a single value. This argument must not be used if axis is a mapping.
65836584
inplace : bool, optional
65846585
Whether or not to modify the original object or return a new array and leave the original intact.
65856586
Defaults to False.
@@ -6595,48 +6596,55 @@ def set_labels(self, axis=None, labels=None, inplace=False, **kwargs):
65956596
--------
65966597
>>> a = ndtest('nat=BE,FO;sex=M,F')
65976598
>>> a
6598-
nat\\sex M F
6599+
nat\sex M F
65996600
BE 0 1
66006601
FO 2 3
6601-
>>> a.set_labels(X.sex, ['Men', 'Women'])
6602-
nat\\sex Men Women
6602+
>>> a.set_labels('sex', ['Men', 'Women'])
6603+
nat\sex Men Women
66036604
BE 0 1
66046605
FO 2 3
66056606
66066607
when passing a single string as labels, it will be interpreted to create the list of labels, so that one can
66076608
use the same syntax than during axis creation.
66086609
6609-
>>> a.set_labels(X.sex, 'Men,Women')
6610-
nat\\sex Men Women
6610+
>>> a.set_labels('sex', 'Men,Women')
6611+
nat\sex Men Women
66116612
BE 0 1
66126613
FO 2 3
66136614
66146615
to replace only some labels, one must give a mapping giving the new label for each label to replace
66156616
6616-
>>> a.set_labels(X.sex, {'M': 'Men'})
6617-
nat\\sex Men F
6617+
>>> a.set_labels('sex', {'M': 'Men'})
6618+
nat\sex Men F
66186619
BE 0 1
66196620
FO 2 3
66206621
6622+
to transform labels by a function, use any function accepting and returning a single argument:
6623+
6624+
>>> a.set_labels('nat', str.lower)
6625+
nat\sex M F
6626+
be 0 1
6627+
fo 2 3
6628+
66216629
to replace labels for several axes at the same time, one should give a mapping giving the new labels for each
66226630
changed axis
66236631
66246632
>>> a.set_labels({'sex': 'Men,Women', 'nat': 'Belgian,Foreigner'})
6625-
nat\\sex Men Women
6633+
nat\sex Men Women
66266634
Belgian 0 1
66276635
Foreigner 2 3
66286636
66296637
or use keyword arguments
66306638
66316639
>>> a.set_labels(sex='Men,Women', nat='Belgian,Foreigner')
6632-
nat\\sex Men Women
6640+
nat\sex Men Women
66336641
Belgian 0 1
66346642
Foreigner 2 3
66356643
66366644
one can also replace some labels in several axes by giving a mapping of mappings
66376645
66386646
>>> a.set_labels({'sex': {'M': 'Men'}, 'nat': {'BE': 'Belgian'}})
6639-
nat\\sex Men F
6647+
nat\sex Men F
66406648
Belgian 0 1
66416649
FO 2 3
66426650
"""
@@ -6656,6 +6664,8 @@ def set_labels(self, axis=None, labels=None, inplace=False, **kwargs):
66566664
real_axis = self.axes[old_axis]
66576665
if isinstance(axis_changes, dict):
66586666
new_axis = real_axis.replace(axis_changes)
6667+
elif callable(axis_changes):
6668+
new_axis = real_axis.apply(axis_changes)
66596669
else:
66606670
new_axis = Axis(axis_changes, real_axis.name)
66616671
new_axes.append((old_axis, new_axis))

0 commit comments

Comments
 (0)