2017-07-06 37 views
5

换档时的整数列,我知道如何解决我的专栏时,大熊猫会自动转换的整数,因为NaN的存在对浮动。 I basically use the method described here.大熊猫移将转换int类型为浮动并四舍五入

但是,如果移位介绍为NaN从而将所有整数花车,有一些舍入出现这种情况(例如上划时代的时间戳),所以即使重铸回整不复制什么原是。

任何方式解决这一问题?

示例数据:

pd.DataFrame({'epochee':[1495571400259317500,1495571400260585120,1495571400260757200, 1495571400260866800]}) 
Out[19]: 
       epoch 
0 1495571790919317503 
1 1495999999999999999 
2 1495571400265555555 
3 1495571400267777777 

示例代码:

df['prior_epochee'] = df['epochee'].shift(1) 
df.dropna(axis=0, how='any', inplace=True) 
df['prior_epochee'] = df['prior_epochee'].astype(int) 

结果输出:

Out[22]: 
       epoch   prior_epoch 
1 1444444444444444444 1400000000000000000 
2 1433333333333333333 1490000000000000000 
3 1777777777777777777 1499999999999999948 
+0

你能提供下这个现象发生例如数据转移自己吗? –

+0

@Cedric看到更新后的问题 – guy

回答

1

因为你知道,当int被铸造为float由于np.nan会发生什么你知道你不希望np.nan行,无论如何,你可以用numpy

df[1:].assign(prior_epoch=df.epoch.values[:-1]) 

       epoch   prior_epoch 
1 1495571400260585120 1495571400259317500 
2 1495571400260757200 1495571400260585120 
3 1495571400260866800 1495571400260757200