AttributeError: 'NoneType' object has no attribute 'dtypes'

Question:

I am trying to sort a subset of my data frame in increasing order with respect to the date index. When I run the sort function only by specifying the axis the code runs. When I add inplace=True I get the error message: AttributeError Traceback (most recent call last) in
—-> 1 daily_data.dtypes

AttributeError: ‘NoneType’ object has no attribute ‘dtypes’

How can I fix the error without removing inplace=True?

column_subset= ['date','from_municipality','from_municipality_number','to_municipality','to_municipality_number','count']
daily_data = df[column_subset]

daily_data = daily_data.set_index(pd.DatetimeIndex(daily_data['date']))
daily_data.drop(['date'], axis=1, inplace=True)
daily_data.replace(to_replace='*', value=np.nan, inplace=True)
daily_data.head()

daily_data = daily_data.sort_index(axis = 0, inplace=True) 

daily_data.dtypes
Asked By: Elda

||

Answers:

If you use inplace=True, don’t assign it the result (it’ll be None):

daily_data.sort_index(axis=0, inplace=True)
daily_data.dtypes
Answered By: L3viathan

Instead of daily_data = daily_data.sort_index(axis = 0, inplace=True) remove the self assignment and just use daily_data.sort_index(axis = 0, inplace=True) as it does the sort "inplace".