Python Apply passing arguments to function works for one function but not a second

Viewed 79

I have a data frame that contains hundreds of stock tickers along with price information for each ticker each day. It looks something like this:

   ticker    date       open     high     low     close      volume     
0   ZEST  2011-01-03  537.500  537.500  537.500  537.500         2.0            
1   WHG   2011-01-03   40.230   40.660   40.110   40.500      9200.0      
2   ZEST  2011-01-04   31.300   31.660   31.160   31.580  34397100.0      
3   WHG   2011-01-04   17.030   17.150   16.870   16.900    621800.0    
4   ZEST  2011-01-05   31.230   31.230   30.960   31.030   1273300.0    
5   WHG   2011-01-05   31.230   31.230   30.960   31.030   1273300.0      

I am trying to calculate indicator values for each security so I am creating a group and functions and applying the function to the group:

import pandas as pd
import talib as ta

df = pd.read_csv(r"C:\BulkInsert.csv", index_col=False) 
def my_ATR(df):  
     
    return ta.ATR(  
        high=df.high,
        low=df.low,
        close=df.close,
        timeperiod=14  
        )
def my_RSI(df):  
     
    return ta.RSI(
        close=df.close,
        timeperiod=14  
        )
new_ATR = df.groupby("ticker", as_index=False).apply(my_ATR)
df['ATR'] = new_ATR.reset_index(level=0, drop=True)

new_RSI = df.groupby("ticker", as_index=False).apply(my_RSI)
df['RSI'] = new_RSI.reset_index(level=0, drop=True)        

The my_ATR function calculates an ATR value on the grouped securities without issue but the my_RSI function returns the following error:

Traceback (most recent call last):
  File "C:\Users\noozak\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\groupby\groupby.py", line 1253, in apply
    result = self._python_apply_general(f, self._selected_obj)
  File "C:\Users\noozak\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\groupby\groupby.py", line 1287, in _python_apply_general
    keys, values, mutated = self.grouper.apply(f, data, self.axis)
  File "C:\Users\noozak\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\groupby\ops.py", line 783, in apply
    result_values, mutated = splitter.fast_apply(f, sdata, group_keys)
  File "C:\Users\noozak\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\groupby\ops.py", line 1328, in fast_apply
    return libreduction.apply_frame_axis0(sdata, f, names, starts, ends)
  File "pandas\_libs\reduction.pyx", line 381, in pandas._libs.reduction.apply_frame_axis0
  File "c:\Users\noozak\OneDrive\Desktop\APP\.vscode\test1.py", line 39, in my_RSI
    return ta.RSI(
  File "C:\Users\noozak\AppData\Local\Programs\Python\Python39\lib\site-packages\talib\__init__.py", line 35, in wrapper
    result = func(*args, **kwargs)
  File "_func.pxi", line 4344, in talib._ta_lib.RSI
TypeError: RSI() takes at least 1 positional argument (0 given)

I am not sure what is different between these two lines and why the arguments are passed to my_ATR and not my_RSI.

0 Answers
Related