2014-02-26 8 views
6

변경 : 내가 특별히 지정된추가 행이 내가 가진 문제는 DataFrame에 행을 추가하는 열 DTYPE 변화이다 DTYPE

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

DTYPE가 INT32 일에 (즉, 'I4')로 캔 알 : I가 D 지정 시도한

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

:

>>> df.dtypes 
a int32 
dtype: object 

그러나 연속 첨가 변경하는 D- 타입 float64 내가 추가 한 값의 유형 :

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

하지만 작동하지 않습니다. 새로운 객체를 반환하는 함수를 사용하지 않고도 해결책이 있습니까?

답변

7

확대는 2 단계로 이루어지며 nan이 먼저 해당 열에 배치 된 다음 할당됨에 따라 강제 적용됩니다. 버그/개선 목록에 추가하겠습니다. 그것의 조금 사소한.

다음은 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 

버그에 대한 문제/향상은 자동적으로이 작업을 수행하려면 다음 NaN이 그것을 강요하는 이유에 대한 몇 가지 색상을 원하는 사람을 위해 https://github.com/pydata/pandas/issues/6485

+1

는 플로트하기 : http://pandas.pydata.org/pandas -docs/stable/gotchas.html # support-for-integer-na (나는이 문제로 어려움을 겪고있었습니다) – fantabolous

관련 문제