【问题标题】:Losing timezone-awareness when saving hyerarchical pandas DatetimeIndex to hdf5 in Python在 Python 中将分层熊猫 DatetimeIndex 保存到 hdf5 时失去时区意识
【发布时间】:2014-07-17 13:43:39
【问题描述】:

我正在使用 pandas 0.14.1。假设我需要使用时区在分层索引中按两个时间戳对数据进行索引。将结果 DataFrame 保存到 hdf5 时,我似乎失去了时区意识:

import pandas as pd
dti1 = pd.DatetimeIndex(start=pd.Timestamp('20000101'), end=pd.Timestamp('20000102'), freq='D', tz='EST5EDT')
dti2 = pd.DatetimeIndex(start=pd.Timestamp('20000102'), end=pd.Timestamp('20000103'), freq='D', tz='EST5EDT')
mux = pd.MultiIndex.from_arrays([dti1, dti2])
df = pd.DataFrame(0, index=mux, columns=['a'])

这里df 有时区:

                                                     a
2000-01-01 00:00:00-05:00 2000-01-02 00:00:00-05:00  0
2000-01-02 00:00:00-05:00 2000-01-03 00:00:00-05:00  0

保存并加载到 hdf5 后,时区信息似乎消失了:

df.to_hdf('/tmp/my.h5', 'data')
pd.read_hdf('/tmp/my.h5', 'data')

结果:

                                         a
2000-01-01 05:00:00 2000-01-02 05:00:00  0
2000-01-02 05:00:00 2000-01-03 05:00:00  0

我想知道是否有一个好的解决方法,以及这是否是一个已知的错误。

【问题讨论】:

    标签: python pandas timestamp hdf5 timestamp-with-timezone


    【解决方案1】:

    使用多索引时,fixed 格式不支持此功能。我想可能应该提出我认为的未实施。这是一个跟踪this的问题

    查看完整的 hdf5 接口文档here

    In [11]: pd.read_hdf('/tmp/my.h5', 'data').index.levels[0]
    Out[11]: 
    <class 'pandas.tseries.index.DatetimeIndex'>
    [2000-01-01 05:00:00, 2000-01-02 05:00:00]
    Length: 2, Freq: None, Timezone: None
    

    但如果你指定table 格式,它就可以工作。

    In [13]: df.to_hdf('/tmp/my.h5', 'data2', format='table')
    
    In [14]: pd.read_hdf('/tmp/my.h5', 'data2')
    Out[14]: 
                                                         a
    2000-01-01 00:00:00-05:00 2000-01-02 00:00:00-05:00  0
    2000-01-02 00:00:00-05:00 2000-01-03 00:00:00-05:00  0
    
    In [15]: pd.read_hdf('/tmp/my.h5', 'data2').index.levels[0]
    Out[15]: 
    <class 'pandas.tseries.index.DatetimeIndex'>
    [2000-01-01 00:00:00-05:00, 2000-01-02 00:00:00-05:00]
    Length: 2, Freq: None, Timezone: EST5EDT
    
    In [16]: pd.read_hdf('/tmp/my.h5', 'data2').index.levels[1]
    Out[16]: 
    <class 'pandas.tseries.index.DatetimeIndex'>
    [2000-01-02 00:00:00-05:00, 2000-01-03 00:00:00-05:00]
    Length: 2, Freq: None, Timezone: EST5EDT
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    • 2013-02-18
    • 2013-05-13
    • 2020-12-27
    • 1970-01-01
    • 2016-01-09
    相关资源
    最近更新 更多