【问题标题】:Pandas HDFStore: Saving and Retrieving a Series with Hierarchical Period IndexPandas HDFStore:使用分层周期索引保存和检索系列
【发布时间】:2016-01-08 02:34:44
【问题描述】:

我首先创建了一个具有二维层次索引的系列。它们的索引类型是 (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 位))

【问题讨论】:

    标签: python numpy pandas indexing hdfstore


    【解决方案1】:

    这未实现请参阅问题here

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-12
      • 2015-01-16
      • 1970-01-01
      • 1970-01-01
      • 2017-05-19
      • 1970-01-01
      • 1970-01-01
      • 2019-10-17
      相关资源
      最近更新 更多