Home > OS >  Tulipy Cannot convert Series to numpy.ndarray
Tulipy Cannot convert Series to numpy.ndarray

Time:10-01

During the installation of tulipy i've seen that my numpy version was obsolete, but I tried to upgrade and I checked that it was at the last version. The problem is that if i call:

    import tulipy as ti   
    def view(data, ma_check, ma_lenght):
        if ma_check == 1:
            ma = ti.sma(data, ma_lenght)
    ma = view(data['close'], 1, 20)
    print(ma)

output:

TypeError: Cannot convert Series to numpy.ndarray

but if I put data['close'].values instead of data['close'] (so converting the series before tulipy does) inside, the function works perfectly.
Is tulipy recognizing another version of numpy? How can I fix the error?

CodePudding user response:

The error does not concern the version of numpy. As you can see from the error message, the error is a TypeError: i.e. you are passing the wrong type. As stated in the documentation for tulipy:

Tulipy requires numpy as all inputs and outputs are numpy arrays (dtype=np.float64).

Yet with data['close'] you are passing a pd.Series. The function call works with pd.Series.values, since this "[return[s] Series as ndarray or ndarray-like depending on the dtype".

data = pd.DataFrame({'close': [0.1,0.2]})

print(type(data['close']))
<class 'pandas.core.series.Series'>

print(type(data['close'].values))
<class 'numpy.ndarray'>

N.B. It is now recommended that you use pd.Series.to_numpy instead of pd.Series.values. Note also that the dtype needs to be np.float64. So, you can check this as well:

print(type(data['close'].to_numpy().dtype))
<class 'numpy.dtype[float64]'>
  • Related