2014-10-28 17 views
0

在熊貓中,我使用貨幣工作了很多。到目前爲止,我一直在使用默認的浮點數,但處理精度不足的問題令人討厭並且容易出錯。我試圖切換到使用Decimal來處理某些部分,雖然它可能會使計算速度變慢很多,但確實如此。然而,當我嘗試保存到一個大熊貓存儲(例如hdf5store通過pytables)我得到: TypeError: Cannot serialize the column [o] because its data contents are [mixed] object dtype以精確的方式將貨幣保存到熊貓商店(如Decimal?)

這裏是我想要做一個簡短的樣本:

import pandas as pd 
from decimal import Decimal 
teststore = pd.HDFStore('teststore.h5') 
df = pd.DataFrame(data={'o':[Decimal('5.1')]}) 
teststore['test'] = df 

..這提高了上面的例外。 df.convert_objects(convert_numeric=True)沒有幫助。

有沒有辦法將Decimal保存到熊貓商店,如果不是,有沒有一種推薦的方式來精確地將貨幣存儲在熊貓商店中?

我使用python 2.7.8,pandas 0.14.1和pytables 3.1.1。

回答

1

適用於0.15.0。儘管它基本上是作爲一個實際的python對象進行醃製的,所以使用HDF5幾乎沒有任何好處。

In [46]: from decimal import Decimal 

In [47]: teststore = pd.HDFStore('teststore.h5') 

In [48]: df = pd.DataFrame(data={'o':[Decimal('5.1')]}) 

In [49]: teststore['test'] = df 
pandas/io/pytables.py:2487: PerformanceWarning: 
your performance may suffer as PyTables will pickle object types that it cannot 
map directly to c-types [inferred_type->mixed,key->block0_values] [items->['o']] 

    warnings.warn(ws, PerformanceWarning) 

作爲一個僅供參考,一般float64有14-16位的精度,所以不知道你爲什麼不使用它們(您可能需要更改顯示打印精度看到它)。

In [50]: In [34]: pd.set_option('precision',16) 

In [51]: In [35]: s = Series([0.0000000000001,0.000000000000002]) 

In [52]: s+s 
Out[52]: 
0 0.000000000000200 
1 0.000000000000004 
dtype: float64 
+0

非常酷,它工作在0.15.0和性能警告注意。 – fantabolous 2014-10-29 04:09:18

+0

re np.float64 vs decimal:我不需要這樣的精度水平 - 通常最多我需要低至0.0001。例如,如果我從0開始並加1分(0.01)十次,結果爲10美分(0.1),則結果與直接指定0.1不同。我最終不得不建造和使用一個小型的「軟」比較器等庫,這些庫允許少量的錯誤,這些錯誤比較慢並且使用起來很麻煩。在SO和其他地方有很多關於這個的線索,推薦的解決方案通常似乎是Decimal,儘管我沒有發現任何特定的numpy/pandas。 – fantabolous 2014-10-29 04:21:00