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
278 views
in Technique[技术] by (71.8m points)

python - Adding row to pandas DataFrame changes dtype

The problem I have is that adding a row to DataFrame changes dtype of columns:

>>> from pandas import DataFrame
>>> df = DataFrame({'a' : range(10)}, dtype='i4')
>>> df
   a
0  0
1  1
2  2
3  3
4  4
5  5
6  6
7  7
8  8
9  9

[10 rows x 1 columns]

I specifically specified dtype to be int32 (i.e., 'i4'), as can be seen:

>>> df.dtypes
a    int32
dtype: object

However, adding a row changes dtype to float64:

>>> df.loc[10] = 99

>>> df
     a
0    0
1    1
2    2
3    3
4    4
5    5
6    6
7    7
8    8
9    9
10  99

[11 rows x 1 columns]

>>> df.dtypes
a    float64
dtype: object

I've tried specifying the dtype of the value that I add:

>>> import numpy as np
>>> df = DataFrame({'a' : np.arange(10, dtype=np.int32)})

>>> df.dtypes
a    int32
dtype: object

>>> df.loc[10] = np.int32(0)

>>> df.dtypes
a    float64
dtype: object

But that does not work either. Is there any solution, without using functions that return new objects?

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Enlargment is done in 2 stages, and a nan is placed in that column first, then its assigned, so that is why it is coerced. I'll put it on the bug/enhancement list. Its a bit non-trivial.

Here's a workaround, by using append.

In [14]: df.append(Series(99,[10],dtype='i4').to_frame('a'))
Out[14]: 
     a
0    0
1    1
2    2
3    3
4    4
5    5
6    6
7    7
8    8
9    9
10  99

[11 rows x 1 columns]

In [15]: df.append(Series(99,[10],dtype='i4').to_frame('a')).dtypes
Out[15]: 
a    int32
dtype: object

An issue for the bug/enhancement to do this automagically: https://github.com/pydata/pandas/issues/6485


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

...