Skip to main content
 首页 » 编程设计

python之Pandas HDFStore : Saving and Retrieving a Series with Hierarchical Period Index

2024年10月24日14TianFang

我首先创建一个具有二维层次索引的系列。它们的索引类型是 (pandas.period, numpy.int32)

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 保存系列 (ts1) 并检索它(作为 ts2):

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'] 
     ...:      

现在,检索到的系列 (ts2) 的索引的 dtype 已更改为整数:

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 

有没有办法正确保存系列?我知道我可以在检索数据后更改类型,但我更愿意干净利落地完成这项工作。

我正在使用 pandas 0.16.1 和 pyhon 2.7.7(Anaconda 2.0.1(64 位))

请您参考如下方法:

这未实现,请参阅问题 here

保存为时间戳并在回读后转换为句点