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
Using the inplace=True keyword in a pandas method changes the default behaviour such that the operation on the dataframe doesn't return anything, it instead 'modifies the underlying data' (more on that later). It mutates the actual object which you apply it to.
When trying to make changes to a Pandas dataframe using a function, we use 'inplace=True' if we want to commit the changes to the dataframe. Therefore, the first line in the following code changes the name of the first column in 'df' to 'Grades'. We need to call the database if we want to see the resulting database.
The fillna() method replaces the NULL values with a specified value. The fillna() method returns a new DataFrame object unless the inplace parameter is set to True , in that case the fillna() method does the replacing in the original DataFrame instead.
We can clearly see that there are no changes in the original dataframe. Through this, we conclude that the default value of inplace is False.
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!!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With