Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

python 3.x - Pandas dataframe: set_index with inplace=True returns a NoneType, why?

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!!!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...