TypeError: Argument has incorrect type (expected numpy.ndarray, got DataFrame) Solution
After upgrading packages in a conda env, talib cannot accept
dataframe as input, the error message looks like
TypeError: Argument 'xxx' has incorrect type (expected numpy.ndarray, got DataFrame)
:
Traceback (most recent call last):
File "/data/1.py", line 7, in <module>
df['SMA_5'] = ta.SMA(df['Close'], timeperiod=5)
File "/data/miniconda3/envs/a/lib/python3.10/site-packages/talib/__init__.py", line 64, in wrapper
result = func(*_args, **_kwds)
TypeError: Argument 'real' has incorrect type (expected numpy.ndarray, got DataFrame)
Most of the web search results are misleading, like changing the df into np array. Since the code works before updating packages, the problem should be package incompatibility issue.
The problem can be easily reproduced by the following code:
import talib as ta
import yfinance as yf
df = yf.download("MSFT", period='5d')
df['SMA'] = ta.SMA(df['Close'], timeperiod=3)
Using $ conda list
to check the version diff:
Not work version:
python 3.10.13 hd12c33a_1_cpython conda-forge
pandas 2.1.4 py310h1128e8f_0
ta-lib 0.4.32 py310h8a78493_0 conda-forge
yfinance 0.2.48 pyhd8ed1ab_0 conda-forge
Work version:
python 3.8.20 he870216_0
pandas 1.5.3 py38h417a72b_0
ta-lib 0.4.32 py38he82f83a_0 conda-forge
yfinance 0.2.40 pyhd8ed1ab_0 conda-forge
BTW, it's not that obvious to find that the root cause is yfinance instead of talib and pandas.
Finally found that talib is work for yfinance 0.2.44 but not work for 0.2.46+.
The solution is to downgrade the yfinance to 0.2.44:
$ conda install yfinance=0.2.44
Then talib and pandas works perfectly.
Posted an github issue to yfinance: https://github.com/ranaroussi/yfinance/issues/2107