Skip to content

Commit 33bb146

Browse files
committed
implemented Axis.apply
1 parent 65d8361 commit 33bb146

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

doc/source/api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Modifying/Selecting
6464
Axis.extend
6565
Axis.insert
6666
Axis.replace
67+
Axis.apply
6768
Axis.union
6869
Axis.intersection
6970
Axis.difference

doc/source/changes/version_0_30.rst.inc

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

44
* new syntax
@@ -96,7 +96,12 @@ New features
9696
* added a feature (see the :ref:`miscellaneous section <misc>` for details).
9797

9898

99-
.. _misc:
99+
* implemented :py:obj:`Axis.apply()` method to transform an axis labels by a function and return a new Axis.
100+
101+
>>> sex = Axis('sex=MALE,FEMALE')
102+
>>> sex.apply(str.capitalize)
103+
Axis(['Male', 'Female'], 'sex')
104+
100105

101106
Miscellaneous improvements
102107
--------------------------

larray/core/axis.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,28 @@ def replace(self, old, new=None):
10551055
labels[indices] = new
10561056
return Axis(labels, self.name)
10571057

1058+
def apply(self, func):
1059+
"""
1060+
Returns a new axis with the labels transformed by func.
1061+
1062+
Parameters
1063+
----------
1064+
func : callable
1065+
A callable which takes a single argument and returns a single value.
1066+
1067+
Returns
1068+
-------
1069+
Axis
1070+
a new Axis with the transformed labels.
1071+
1072+
Examples
1073+
--------
1074+
>>> sex = Axis('sex=MALE,FEMALE')
1075+
>>> sex.apply(str.capitalize)
1076+
Axis(['Male', 'Female'], 'sex')
1077+
"""
1078+
return Axis(map(func, self.labels), self.name)
1079+
10581080
# XXX: rename to named like Group?
10591081
def rename(self, name):
10601082
"""

0 commit comments

Comments
 (0)