Python Forum

Full Version: Why reindex converts integer to decimal?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I have the following code:

frame = pd.DataFrame(np.arange(9).reshape((3,3)),
                     index=['a', 'c', 'd'],
                     columns=['Ohio', 'Texas', 'California'])
So the elements in the DataFrame are integers from 0 to 8 as indicated below:

Ohio Texas California
a 0 1 2
c 3 4 5
d 6 7 8

However, when I executed:
 frame2 = frame.reindex(['a', 'b', 'c', 'd']) 
, I got:

Ohio Texas California
a 0.0 1.0 2.0
b NaN NaN NaN
c 3.0 4.0 5.0
d 6.0 7.0 8.0


Q1: Why the elements became decimals?

Strangely, when I typed the following:

states = ['Texas', 'Utah', 'California']                                                                                                 
frame.reindex(columns=states)                                                                                                            
I got the following with the elements back to integer.

Texas Utah California
a 1 NaN 2
c 4 NaN 5
d 7 NaN 8


When I executed:

 frame.loc[['a', 'b', 'c', 'd'], states]      
Texas Utah California
a 1.0 NaN 2.0
b NaN NaN NaN
c 4.0 NaN 5.0
d 7.0 NaN 8.0

As shown above, the elements became decimals again.

How come? Could you please let me know what is going on?
This is behaviour described in help and documentation. Have a look at >>> help(pd.DataFrame.reindex) or pandas.DataFrame.reindex.

If you haven't recently updated pandas then it's good idea to do so. Pandas 1.0.0 was released on 30th of January 2020 (and 1.0.1 on 5th of February).
Thanks. The example in the help document has the same behavior but it does not seem to explain the reason. Does it has something to do with the tolerance optional parameter?

I installed Anaconda Navigator 1.9.7 on a Mac around Jan 24, 2002. I think pandas was installed automatically during the process. I typed "conda update pandas" in xterm and checked using pd.__version__ under ipython but it was like version 0.2. Then, I typed "Conda update --all" under xterm and checked again. I found that version 1.0.0 was installed. How come it did not get updated to 1.0.1?

Anyway, after updating, I re-run the code. The behavior remains the same with an additional error when I executed:
 frame.loc[['a', 'b', 'c', 'd'], states] 
in the last step.

KeyError: 'Passing list-likes to .loc or [] with any missing labels is no longer supported, see https://pandas.pydata.org/pandas-docs/st...x-listlike'