Federico Marchese
Federico Marchese

Reputation: 219

Pandas TypeError: Transform function invalid for data types on max calculation by group

I am switching from R and relatively new to python.

I am trying to accomplish a pretty simple task and get a little more complex later.

I am trying to get the max by group, group being 'symbol' in this case, and create a new column with the max from the 'ClosePrice' column.

I am using groupby and transform:

proccessing_data['Max'] = proccessing_data.groupby('Symbol').transform(lambda x: x.ClosePrice.max(), axis =1)

and return the following error:

TypeError: Transform function invalid for data types

my end goal is actually to use functions within transform:

proccessing_data['RSI'] = proccessing_data.groupby('Symbol').transform(lambda x: talib.RSI(x["ClosePrice"], timeperiod=14), axis =1)

but this returns the same error.

for reference in R I use:

proccessing_data$RSI_SMA <- ave(proccessing_data$ClosePrice, proccessing_data$Symbol,
                            FUN = function(x) RSI(x,n = 14, maType = "SMA"))

Any help would be greatly appreciated, thanks!

Upvotes: 1

Views: 5084

Answers (1)

akuiper
akuiper

Reputation: 215107

You need to specify which column to operate on the grouped data frame before using transform, since each column is passed to transform function individually, see the docs, you can't do x.ClosePrice in the lambda function; For your case:

proccessing_data['Max'] = proccessing_data.groupby('Symbol').ClosePrice.transform('max')
#                       specify the column to transform here ^^^^^^^^^^

proccessing_data['RSI'] = proccessing_data.groupby('Symbol').ClosePrice.transform(lambda x: talib.RSI(x, timeperiod=14))
#                       specify the column to transform here ^^^^^^^^^^

Upvotes: 5

Related Questions