Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Why reindex converts integer to decimal?
#1
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?
Quote
#2
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).
new_to_python likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote
#3
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'
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  reindex() Truman 2 443 Jul-17-2020, 10:21 PM
Last Post: Truman
  reindex dataframe after sorting Clunk_Head 4 1,039 Jun-26-2019, 01:04 AM
Last Post: scidam

Forum Jump:


Users browsing this thread: 1 Guest(s)