Arbitrary operations on DataFrames
apply
- class timeflux.nodes.apply.ApplyMethod(method, apply_mode='universal', axis=0, closed='right', func=None, **kwargs)[source]
Bases:
timeflux.core.node.Node
Apply a function along an axis of the DataFrame.
This node applies a function along an axis of the DataFrame. Objects passed to the function are Series objects whose index is either the DataFrame’s index (
axis
= 0) or the DataFrame’s columns (axis
= 1).- Variables
- Parameters
func (func) – custom function specified directely that takes as input a n_array (eg. lambda x: x+1). Default: None.
method (str) – name of the module to import, in which the method is defined. eg. numpy.mean.
apply_mode (str`) – {universal, reduce, expand }. Default: universal. - universal if function is a transformation from n_array to n_array - reduce if function is a transformation from n_array to scalar - expand if function is a transformation from n_array to nk_array [not yet implemented]
axis (int) – if 0, the transformation is applied to columns, if 1 to rows. Default: 0.
closed (str) – {left, right, center}: timestamp to transfer in the output, only when method_type is “reduce” and axis = 0, in which case, the output port’s lenght is 1. Default: right.
kwargs – additional keyword arguments to pass as keywords arguments to func.
Notes
Note that the passed function will receive ndarray objects for performance purposes. For universal functions, ie. transformation from n_array to n_array, input and output ports have the same size. For reducing function, ie. from n_array to scalar, output ports’s index is set to first (if
closed
= left), last (ifclosed
= right), or middle (ifclosed
= center)Todo
Allow expanding functions such as n_array to nk_array (with XArray usage)
Example
Universal function: in this example, we apply numpy.sqrt to each value of the data. Shapes of input and output data are the same.
method
= numpy.sqrtmethod_type
= universal
If data in input port is
i
is:0 2018-10-25 07:33:41.871131 9.0 2018-10-25 07:33:41.873084 16.0 2018-10-25 07:33:41.875037 1.0 2018-10-25 07:33:41.876990 4.0
It returns the squared root of the data on port
o
:0 2018-10-25 07:33:41.871131 3.0 2018-10-25 07:33:41.873084 4.0 2018-10-25 07:33:41.875037 1.0 2018-10-25 07:33:41.876990 2.0
Example
Reducing function: in this example, we apply numpy.sum to each value of the data. Shapes of input and output data are not the same. We set:
method
= numpy.summethod_type
= reduceaxis
= 0closed
= right
If data in input port is
i
is:0 1 2018-10-25 07:33:41.871131 9.0 10.0 2018-10-25 07:33:41.873084 16.0 2.0 2018-10-25 07:33:41.875037 1.0 5.0 2018-10-25 07:33:41.876990 4.0 2.0
It returns the sum amongst row axis on port
o
:0 1 2018-10-25 07:33:41.876990 30.0 19.0
References
See the documentation of pandas.apply .
Instantiate the node.