Flo*_*ent 4 python pandas ta-lib
我有一个数据框,其中包含几种证券的价格作为列,但我找不到一次性运行 TA-Lib 的解决方案,因为它需要 numpy.ndarray。
如何在多个证券上运行 TA-Lib 并获得数据框作为回报?
import talib as ta
d = {'security1': [1,2,8,9,8,5], 'security2': [3,8,5,4,3,5]}
df = pd.DataFrame(data=d)
df
Out[518]:
security1 security2
0 1 3
1 2 8
2 8 5
3 9 4
4 8 3
5 5 5
ta.EMA(df, 2)
TypeError: Argument 'real' has incorrect type (expected numpy.ndarray, got DataFrame)
ta.EMA(df['security1'], 2)
Out[520]:
0 NaN
1 1.500000
2 5.833333
3 7.944444
4 7.981481
5 5.993827
dtype: float64
type(df['security1'])
Out[524]: pandas.core.series.Series
Run Code Online (Sandbox Code Playgroud)
当我将数据框转换为 numpy.ndarray 时,它仍然抛出异常:
ta.EMA(df.values, 2)
Out[528]: Exception: input array type is not double
Run Code Online (Sandbox Code Playgroud)
谢谢你。
TA-Lib 需要浮点数据,而您的则是不可或缺的。
因此,在构建数据框时,您需要通过指定来强制输入数据dtype=numpy.float64:
import pandas
import numpy
import talib
d = {'security1': [1,2,8,9,8,5], 'security2': [3,8,5,4,3,5]}
df = pandas.DataFrame(data=d, dtype=numpy.float64) # note numpy.float64 here
Run Code Online (Sandbox Code Playgroud)
TA-Lib的预计一个维数组,它可以在操作该装置pandas.Series,但没有pandas.DataFrame。
但是,您可以使用pandas.DataFrame.apply对数据框的每一列应用函数
df.apply(lambda c: talib.EMA(c, 2))
security1 security2
0 NaN NaN
1 1.500000 5.500000
2 5.833333 5.166667
3 7.944444 4.388889
4 7.981481 3.462963
5 5.993827 4.487654
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3658 次 |
| 最近记录: |