2015-10-10 37 views
2

我首先創建一個具有二維分層索引的系列。他們索引類型(pandas.period,numpy.int32)大熊貓HDFStore:保存和檢索具有分層週期索引的系列

In [265]: import pandas as pd 

In [266]: import numpy as np 

In [267]: hdf_file = r'F:\test.h5' 

In [268]: data = np.random.randint(10, size=(7, 3)) 

In [269]: dates = pd.date_range('1/1/2015', '1/7/2015').to_period('D') 

In [270]: ts1 = pd.DataFrame(data, index = dates, columns = [1, 2, 3]).stack() 

然後,我保存使用HDFStore和檢索(如TS2)系列(TS1):

In [271]: with pd.HDFStore(hdf_file, 'w') as store: 
    ...:  store['ts'] = ts1 
    ...:  

In [272]: with pd.HDFStore(hdf_file, 'r') as store: 
    ...:  ts2 = store['ts'] 
    ...:  

現在,D型檢索系列(TS2)的指標已改爲整數:

In [273]: print(ts1) 
2015-01-01 1 3 
      2 8 
      3 0 
2015-01-02 1 2 
      2 3 
      3 9 
2015-01-03 1 9 
      2 2 
      3 2 
2015-01-04 1 4 
      2 5 
      3 1 
2015-01-05 1 2 
      2 1 
      3 6 
2015-01-06 1 1 
      2 0 
      3 8 
2015-01-07 1 0 
      2 6 
      3 8 
dtype: int32 

In [274]: print(ts2) 
16436 1 3 
     2 8 
     3 0 
16437 1 2 
     2 3 
     3 9 
16438 1 9 
     2 2 
     3 2 
16439 1 4 
     2 5 
     3 1 
16440 1 2 
     2 1 
     3 6 
16441 1 1 
     2 0 
     3 8 
16442 1 0 
     2 6 
     3 8 
dtype: int32 

是否有辦法妥善保存該系列?我知道在檢索數據後可以更改類型,但我更願意乾淨地完成這項工作。

我使用熊貓0.16.1和pyhon 2.7.7(蟒蛇2.0.1(64位))

回答

2

這沒有實現看問題here

另存爲時間戳和轉換時期讀回之後