This error happens when you try to reset_index on a pandas Series object with inplace=True. For instance:

 import pandas as pd s = pd.Series([1,2,3])  s.reset_index(inplace=True) 

Will result in this error.

The reason this errors out is that s by itself is a pandas Series, but reset_index will create a dataframe, and inplace change will make the data type before and after in compatible.

Solution:

Solution to this problem is to not use inplace=True but reassign instead, i.e.

 s = s.reset_index() 

This works since it's creating a new variable to hold the data frame instead of trying to mutate the original variable s in place.

To see the full demo: https://akuiper.com/console/pQ39pCFhkifL

Or try it in the PyConsole browser extension:


This post is ad-supported