Python FutureWarning: Indexing with multiple keys (implicitly converted to a tuple of keys)

Viewed 6133

I've recently upgraded Python to 3.7.6 and my existing code:

df['Simple_Avg_Return'] = df.groupby(['YF_Ticker'])['Share_Price_Delta_Percent', 'Dividend_Percent'].transform(
        sum).divide(2).round(2) 

Is now throwing this warning:

FutureWarning: Indexing with multiple keys (implicitly converted to a
tuple of keys) will be deprecated, use a list instead.  

How would I go about converting this to a list as advised and where?

1 Answers

You need to use an extra bracket around ['Share_Price_Delta_Percent', 'Dividend_Percent']

Like this,

df['Simple_Avg_Return'] = df.groupby(['YF_Ticker'])[['Share_Price_Delta_Percent', 'Dividend_Percent']].transform(
        sum).divide(2).round(2) 

Quoting @ALollz comment

The decision was made https://github.com/pandas-dev/pandas/issues/23566. To keep compatibility between 0.25 and 1.0 they didn't remove the feature but added a warning in 1.0. Likely it will be removed in the next major deprecation cycle.

Source

Related