temporian.EventSet.map #
map(
func: MapFunction,
output_dtypes: Optional[TargetDtypes] = None,
receive_extras: bool = False,
) -> EventSetOrNode
Applies a function on each value of an
EventSet
's features.
The function receives the scalar value, and if receive_extras
is True,
also a MapExtras
object containing
information about the value's position in the EventSet. The MapExtras
object should not be modified by the function, since it is shared across
all calls.
If the output of the functon has a different dtype than the input, the
output_dtypes
argument must be specified.
This operator is slow. When possible, existing operators should be used.
A Temporian graph with a map
operator is not serializable.
Usage example with lambda function
>>> a = tp.event_set(
... timestamps=[0, 1, 2],
... features={"value": [10, 20, 30]},
... )
>>> b = a.map(lambda v: v + 1)
>>> b
indexes: ...
(3 events):
timestamps: [0. 1. 2.]
'value': [11 21 31]
...
Usage example with output_dtypes
:
>>> a = tp.event_set(
... timestamps=[0, 1, 2],
... features={"a": [10, 20, 30], "b": ["100", "200", "300"]},
... )
>>> def f(value):
... if value.dtype == np.int64:
... return float(value) + 1
... else:
... return int(value) + 2
>>> b = a.map(f, output_dtypes={"a": float, "b": int})
>>> b
indexes: ...
(3 events):
timestamps: [0. 1. 2.]
'a': [11. 21. 31.]
'b': [102 202 302]
...
Usage example with MapExtras
:
>>> a = tp.event_set(
... timestamps=[0, 1, 2],
... features={"value": [10, 20, 30]},
... )
>>> def f(value, extras):
... return f"{extras.feature_name}-{extras.timestamp}-{value}"
>>> b = a.map(f, output_dtypes=str, receive_extras=True)
>>> b
indexes: ...
(3 events):
timestamps: [0. 1. 2.]
'value': [b'value-0.0-10' b'value-1.0-20' b'value-2.0-30']
...
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
MapFunction
|
The function to apply on each value. |
required |
output_dtypes |
Optional[TargetDtypes]
|
Expected dtypes of the output feature(s) after
applying the function to them. If not provided, the output
dtypes will be expected to be the same as the input ones. If a
single dtype, all features will be expected to have that dtype.
If a mapping, the keys can be either feature names or the
input dtypes (and not both types mixed), and the values are the
target dtypes for them. All dtypes must be Temporian types (see
|
None
|
receive_extras |
bool
|
Whether the function should receive a
|
False
|
Returns:
Type | Description |
---|---|
EventSetOrNode
|
EventSet with the function applied on each value. |