Skip to content

Commit 4910499

Browse files
committed
implemented LArray.apply_map
1 parent e22b022 commit 4910499

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

doc/source/api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ Modifying/Selecting
320320
LArray.ignore_labels
321321
LArray.filter
322322
LArray.apply
323+
LArray.apply_map
323324

324325
.. _la_axes_labels:
325326

doc/source/changes/version_0_30.rst.inc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. py:currentmodule:: larray
1+
.. py:currentmodule:: larray
22

33

44
Syntax changes
@@ -133,6 +133,9 @@ New features
133133
along some axes of an array and return the result. This is an extremely versatile method as it can be used both with
134134
aggregating functions or element-wise functions.
135135

136+
* implemented :py:obj:`LArray.apply_map()` method to apply a transformation mapping to array elements. For example, this
137+
can be used to transform some numeric codes to labels.
138+
136139
* implemented :py:obj:`Axis.apply()` method to transform an axis labels by a function and return a new Axis.
137140

138141
>>> sex = Axis('sex=MALE,FEMALE')

larray/core/array.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
# * use larray "utils" in LIAM2 (to avoid duplicated code)
3030

31-
from collections import Iterable, Sequence, OrderedDict, abc
31+
from collections import Iterable, Sequence, OrderedDict
3232
from itertools import product, chain, groupby, islice, repeat
3333
import os
3434
import sys
@@ -7978,6 +7978,57 @@ def apply(self, transform, *args, **kwargs):
79787978
# transpose back axis where it was
79797979
return res_arr.transpose(self.axes & res_arr.axes)
79807980

7981+
def apply_map(self, mapping, dtype=None):
7982+
r"""
7983+
Apply a transformation mapping to array elements.
7984+
7985+
Parameters
7986+
----------
7987+
mapping : mapping (dict)
7988+
Mapping to apply to values of the array.
7989+
A mapping (dict) must have the values to transform as keys and the new values as values, that is:
7990+
{<oldvalue1>: <newvalue1>, <oldvalue2>: <newvalue2>, ...}.
7991+
dtype : type, optional
7992+
Output dtype. Defaults to None (inspect all output values to infer it automatically).
7993+
7994+
Returns
7995+
-------
7996+
LArray
7997+
Axes will be the same as the original array axes.
7998+
7999+
Notes
8000+
-----
8001+
To apply a transformation given as an LArray (with current values as labels on one axis of
8002+
the array and desired values as the array values), you can use: ``mapping_arr[original_arr]``.
8003+
8004+
Examples
8005+
--------
8006+
First let us define a test array
8007+
8008+
>>> arr = LArray([[0, 2, 1],
8009+
... [3, 1, 5]], 'a=a0,a1;b=b0..b2')
8010+
>>> arr
8011+
a\b b0 b1 b2
8012+
a0 0 2 1
8013+
a1 3 1 5
8014+
8015+
Now, assuming for a moment that the values of our test array above were in fact some numeric representation of
8016+
names and we had the correspondence to the actual names stored in a dictionary:
8017+
8018+
>>> code_to_names = {0: 'foo', 1: 'bar', 2: 'baz',
8019+
... 3: 'boo', 4: 'far', 5: 'faz'}
8020+
8021+
We could get back an array with the actual names by using:
8022+
8023+
>>> arr.apply_map(code_to_names)
8024+
a\b b0 b1 b2
8025+
a0 foo baz bar
8026+
a1 boo bar faz
8027+
"""
8028+
def transform(v):
8029+
return mapping.get(v, v)
8030+
return self.apply(transform, dtype=dtype)
8031+
79818032

79828033
def larray_equal(a1, a2):
79838034
import warnings

0 commit comments

Comments
 (0)