ta-lib-python
ta-lib-python copied to clipboard
Failed to use ta-lib as Polars expression
I am trying to use ta-lib as a Polars expression so that I could possibly leverage the cool parallel processing feature of Polars. Here is the sample code I used to do this
###################### import talib import polars as pl import yfinance as yf
tesla = yf.Ticker('TSLA') tesla_data = tesla.history(period="1Y") tesla_data["Date"]=tesla_data.index pl_df = pl.from_pandas(tesla_data[["Date", "Open", "High", "Low", "Close", "Volume"]])
Method 1. Using ta-lib as a direct function call.
mv_kama = talib.KAMA(pl_df["Close"], 30)
Method 2. Using ta-lib as Polars expression
def kama30() -> pl.Expr: return talib.KAMA(pl.col("Close"), 30)
pl_df2 = pl_df.select([ pl.col("Close"), kama30() ]) ################################
In method 2, ta-lib KAMA function is wrapped as a Polars expression. The code however failed to run properly and the error msg is:
Input In [5], in kama30() 14 def kama30() -> pl.Expr: ---> 15 return talib.KAMA(pl.col("Close"), 30)
File C:\ProgramData\Anaconda3\envs\Charm3.9\lib\site-packages\talib_init_.py:64, in _wrapper.
TypeError: Argument 'real' has incorrect type (expected numpy.ndarray, got Expr)
Appreciate if anyone could advise how this should be done properly.
Thanks!
We don't support polars.Expr right now -- currently only supporting polars.Series and polars.DataFrame... and that support converts those to numpy.ndarray before calling the talib functions.
Contributions welcome!
Here's how to do it with polars.Expr, using any of the TA-Lib releases:
df.select(
[
pl.col("Close").map(lambda s: ta.KAMA(s, 30)),
]
)