otp.Source.apply#
- Source.apply(obj)[source]#
Apply object to data source.
Limitations Integrations with any 3rd party Python packages are not supported. For example, a case when you want to apply some open source ML library. It happens because the code is translated into the OneTick API calls and if there is no support for the 3rd party package then it can’t be integrated.
- Parameters
obj (onetick.py.query, Callable, type, onetick.query.GraphQuery) –
onetick.py.query allows to apply external nested query
Callable allows to apply per tick logic
type allows to apply default type convertation
onetick.query.GraphQuery allows to apply a build onetick.query.Graph
- Return type
Examples
Apply external query to a tick flow. In this case it assumes that query has only one input and one output. Check the
query
examples if you want to use a query with multiple inputs or outputs.>>> data = otp.Ticks(X=[1, 2, 3]) >>> external_query = otp.query('update.otq') >>> data = data.apply(external_query) >>> otp.run(data) Time X 0 2003-12-01 00:00:00.000 2 1 2003-12-01 00:00:00.001 4 2 2003-12-01 00:00:00.002 6
Apply a predicate to a column / operation. In this case value passed to a predicate is column values. Result is a column.
>>> data = otp.Ticks(X=[1, 2, 3]) >>> data['Y'] = data['X'].apply(lambda x: x * 2) >>> otp.run(data) Time X Y 0 2003-12-01 00:00:00.000 1 2 1 2003-12-01 00:00:00.001 2 4 2 2003-12-01 00:00:00.002 3 6
Another example of applying more sophisticated operation
>>> data = otp.Ticks(X=[1, 2, 3]) >>> data['Y'] = data['X'].apply(lambda x: 1 if x > 2 else 0) >>> otp.run(data) Time X Y 0 2003-12-01 00:00:00.000 1 0 1 2003-12-01 00:00:00.001 2 0 2 2003-12-01 00:00:00.002 3 1
Example of applying a predicate to a Source. In this case value passed to a predicate is a whole tick. Result is a column.
>>> data = otp.Ticks(X=[1, 2, 3], Y=[.5, -0.4, .2]) >>> data['Z'] = data.apply(lambda tick: 1 if abs(tick['X'] * tick['Y']) > 0.5 else 0) >>> otp.run(data) Time X Y Z 0 2003-12-01 00:00:00.000 1 0.5 0 1 2003-12-01 00:00:00.001 2 -0.4 1 2 2003-12-01 00:00:00.002 3 0.2 1
See also