When I replace a row of a df, it causes an existing column of dtype=int to become float. I would like to keep it as int.
I create the df:
testdate = pd.datetime(2014, 1, 1)
adddata = {'intcol':0,'floatcol':0.0}
df = pd.DataFrame(data=adddata, index=pd.date_range(testdate, periods=1))
As desired, one column is int and the other is float, as confirmed by df.dtypes
:
floatcol float64
intcol int64
dtype: object
Then I overwrite an existing row (in this case there's only 1) using df.ix[testdate] = pd.Series(adddata)
. I purposely use the same data to show the issue: the intcol has become float. df.dtypes
:
floatcol float64
intcol float64
dtype: object
Note that I can change the cells individually (e.g. df.ix[testdate,'floatcol'] = 0.0
) and the column dtypes are maintained, but in reality I have far more than 2 columns that I want to overwrite simultaneously so doing them one at a time is cumbersome.
interesting that even specifying the data type as object
does not help:
>>> df.loc[testdate,:] = pd.Series(adddata, dtype='object')
>>> df.dtypes
floatcol float64
intcol float64
dtype: object
someone may have a better solution, but i noticed that this works:
>>> df.loc[testdate,:] = pd.Series(list(adddata.values()), adddata.keys(), dtype='object')
>>> df.dtypes
floatcol float64
intcol int64
dtype: object
but, if the row values are in dict
format, probably this would be easier:
>>> df.loc[testdate,:] = list(map(adddata.get, df.columns))
>>> df.dtypes
floatcol float64
intcol int64
dtype: object
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments