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

标签 python numpy pandas indexing hdfstore

我首先创建一个具有二维分层索引的系列。他们的索引类型是(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)的索引的数据类型已更改为整数:

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

保存为时间戳并在读回后转换为周期

关于python - Pandas HDFStore : Saving and Retrieving a Series with Hierarchical Period Index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33050211/

相关文章:

python - 如何在 Python 中对函数进行深层复制?

python - NumPy 数组的反向堆叠操作

python - 从 Panda Dataframe 获取 X Y 值

python - 使用 auto_now_add 更新 mongoengine 中的 DateTime 字段

python - 我如何从这样的模式中提取时间?

python - 在 urllib2.request() 调用上设置超时

python - 加快 np.arange( 1,100) 下值的求和速度

python - 拆分非常大的 Pandas 数据框,替代 Numpy array_split

python - 如何定义下面的 python 作业的 'cars_df' 部分?

python - 值错误 : continuous is not supported