Issue
If I reset the index of my pandas DataFrame with "inplace=True" (following the documentation) it returns a class 'NoneType'. If I reset the index with "inplace=False" it returns the DataFrame with the new index. Why?
print(type(testDataframe))
print(testDataframe.head())
returns:
<class 'pandas.core.frame.DataFrame'>
ALandbouwBosbouwEnVisserij AantalInkomensontvangers AantalInwoners \
0 73780.0 None 16979120
1 290.0 None 25243
2 20.0 None 3555
Set_index returns a new index:
testDataframe = testDataframe.set_index(['Codering'])
print(type(testDataframe))
print(testDataframe.head())
returns
<class 'pandas.core.frame.DataFrame'>
ALandbouwBosbouwEnVisserij AantalInkomensontvangers \
Codering
NL00 73780.0 None
GM1680 290.0 None
WK168000 20.0 None
BU16800000 15.0 None
But the same set_index with "inplace=True":
testDataframe = testDataframe.set_index(['Codering'], inplace=True)
print(type(testDataframe))
print(testDataframe.head())
returns
<class 'NoneType'>
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-50-0d6304ebaae1> in <module>()
Version info:
python: 3.4.4.final.0
python-bits: 64
pandas: 0.18.1
numpy: 1.11.1
IPython: 5.2.2
Solution
Ok, now I understand, thanks for the comments!
So inplace=True should return None and make the change in the original object. It seemed that on listing the dataframe again, no changes were present.
But of course I should not have assigned the return value to the dataframe, i.e.
testDataframe = testDataframe.set_index(['Codering'], inplace=True)
should just be
testDataframe.set_index(['Codering'], inplace=True)
or
testDataframe = testDataframe.set_index(['Codering'], inplace=False)
otherwise the return value of the inplace index change (None) is the new content of the dataframe which is of course not the intend.
I am sure this is obvious to many and now it is to me as well but it wasn't without your help, thanks!!!
Answered By - Wouter
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.