Dry goods丨DolphinDB NumPy tutorial

NumPy is a basic library for scientific computing, often used in conjunction with pandas to achieve complex calculations. The underlying implementation of Orca is based on DolphinDB. If the NumPy function is used to directly process the Orca object, the Orca object will be directly downloaded to the local calculation, causing unnecessary performance loss and may even cause an exception. To this end, Orca provides a subsidiary project, DolphinDB NumPy. It wraps the NumPy interface and is optimized for Orca objects without affecting the use in other situations.

1 installation

The DolphinDB NumPy project has been integrated into the DolphinDB Python API . You can use DolphinDB NumPy by installing DolphinDB Python API through the pip tool.

pip install dolphindb

If you already have an existing NumPy program, you can replace NumPy's import with:

# import numpy as np
import dolphindb.numpy as np

If the program uses orca objects, please ensure that you have connected to the DolphinDB database.

2 Quick start

Create a DolphinDB NumPy ndarray object by passing in a list of values.

>>> import dolphindb.numpy as np 
>>> a = np.array([1, 2])
>>> a
array([1, 2])

If you try to get the type of a, you will find that it is a NumPy ndarray, DolphinDB NumPy is just a wrapper:

>>> type(a)
<class 'numpy.ndarray'>

The use of DolphinDB NumPy is no different from NumPy:

>>> import dolphindb.numpy as np
>>> np.exp(range(5))
array([ 1.        ,  2.71828183,  7.3890561 , 20.08553692, 54.59815003])
>>> np.random.randint(0, 10, 3)
array([4, 7, 8])

DolphinDB NumPy's ndarray objects can be directly operated on with Orca objects. The return result is the Orca intermediate expression.

>>> df = orca.DataFrame({"a": [1,2]})
>>> a + df
<orca.core.operator.ArithExpression object at 0x7ffa4a1d99d0>

3 Functional limitations and precautions of DolphinDB NumPy

DolphinDB NumPy is still in the development stage. If the parameters of DolphinDB NumPy's interface functions include Orca objects, only four arithmetic operations, logical operations, mathematical functions and statistical functions supported by DolphinDB are supported.

When using DolphinDB NumPy functions to manipulate Orca objects, the lazy evaluation strategy used by Orca is used . Therefore, the common four arithmetic operations, logical operations, etc., usually return an intermediate expression:

>>> import dolphindb.numpy as np
>>> a = np.float32(3.5)
>>> df = orca.Series([1,2])
>>> b = a + df
>>> b
<orca.core.operator.ArithExpression object at 0x7ffa4a1d99d0>

>>> b.compute()
0    4.5
1    5.5
dtype: float32

Guess you like

Origin blog.csdn.net/qq_41996852/article/details/112169105