【问题标题】:"Already tz-aware" error when reading h5 file using pandas, python 3 (but not 2)使用 pandas、python 3(但不是 2)读取 h5 文件时出现“已经 tz-aware”错误
【发布时间】:2017-01-31 03:19:54
【问题描述】:

我有一个名为 weather.h5 的 h5 商店。我的默认 Python 环境是 3.5.2。当我尝试阅读这家商店时,我得到TypeError: Already tz-aware, use tz_convert to convert

pd.read_hdf('weather.h5','weather_history')pd.io.pytables.HDFStore('weather.h5')['weather_history] 我都试过了,但无论如何我都会收到错误消息。

我可以在 Python 2.7 环境中打开 h5。这是 Python 3 / pandas 中的错误吗?

【问题讨论】:

  • 请注意,我可以使用 weather_store = pd.io.pytables.HDFStore('weather.h5') 加载 h5,但是当我尝试使用 weather_store['weather_history'] 获取表格时,TypeError 会被引发。

标签: python python-3.x pandas timezone hdf5


【解决方案1】:

我也有同样的问题。我正在使用 Anaconda Python:3.4.5 和 2.7.3。两者都使用 pandas 0.18.1。

这是一个可重现的例子:

generate.py(用 Python2 执行):

import pandas as pd
from pandas import HDFStore

index = pd.DatetimeIndex(['2017-06-20 06:00:06.984630-05:00', '2017-06-20 06:03:01.042616-05:00'], dtype='datetime64[ns, CST6CDT]', freq=None)
p1 = [0, 1]
p2 = [0, 2]

# Saving any of these dataframes cause issues
df1 = pd.DataFrame({"p1":p1, "p2":p2}, index=index)
df2 = pd.DataFrame({"p1":p1, "p2":p2, "i":index})

store = HDFStore("./test_issue.h5")
store['df'] = df1
#store['df'] = df2
store.close()

read_issue.py:

import pandas as pd
from pandas import HDFStore

store = HDFStore("./test_issue.h5", mode="r")
df = store['/df']
store.close()

print(df)

在 Python2 中运行 read_issue.py 没有问题并产生以下输出:

p1  p2

2017-06-20 11:00:06.984630-05:00 0 0 2017-06-20 11:03:01.042616-05:00 1 2

但是在 Python3 中运行它会产生带有此回溯的错误:

Traceback(最近一次调用最后一次): 文件“read_issue.py”,第 5 行,在 df = 商店['df'] getitem 中的文件“/home/denper/anaconda3/envs/py34/lib/python3.4/site-packages/pandas/io/pytables.py”,第 417 行 返回 self.get(key) 文件“/home/denper/anaconda3/envs/py34/lib/python3.4/site-packages/pandas/io/pytables.py”,第 634 行,在 get 返回 self._read_group(group) _read_group 中的文件“/home/denper/anaconda3/envs/py34/lib/python3.4/site-packages/pandas/io/pytables.py”,第 1272 行 返回 s.read(**kwargs) 文件“/home/denper/anaconda3/envs/py34/lib/python3.4/site-packages/pandas/io/pytables.py”,第 2779 行,已读 ax = self.read_index('axis%d' % i) 文件“/home/denper/anaconda3/envs/py34/lib/python3.4/site-packages/pandas/io/pytables.py”,第 2367 行,在 read_index _, index = self.read_index_node(getattr(self.group, key)) 文件“/home/denper/anaconda3/envs/py34/lib/python3.4/site-packages/pandas/io/pytables.py”,第 2492 行,在 read_index_node _unconvert_index(data, kind, encoding=self.encoding), **kwargs) new 中的文件“/home/denper/anaconda3/envs/py34/lib/python3.4/site-packages/pandas/indexes/base.py”,第 153 行 结果=日期时间索引(数据,复制=复制,名称=名称,**kwargs) 包装器中的文件“/home/denper/anaconda3/envs/py34/lib/python3.4/site-packages/pandas/util/decorators.py”,第 91 行 返回函数(*args,**kwargs) new 中的文件“/home/denper/anaconda3/envs/py34/lib/python3.4/site-packages/pandas/tseries/index.py”,第 321 行 raise TypeError("已经 tz-aware,使用 tz_convert " TypeError: 已经 tz-aware,使用 tz_convert 进行转换。 关闭剩余打开的文件:./test_issue.h5...done

因此,索引存在问题。但是,如果您将 df2 保存在 generate.py 中(日期时间作为列,而不是作为索引),则 read_issue.py 中的 Python3 会产生不同的错误:

Traceback(最近一次调用最后一次): 文件“read_issue.py”,第 5 行,在 df = 商店['/df'] getitem 中的文件“/home/denper/anaconda3/envs/py34/lib/python3.4/site-packages/pandas/io/pytables.py”,第 417 行 返回 self.get(key) 文件“/home/denper/anaconda3/envs/py34/lib/python3.4/site-packages/pandas/io/pytables.py”,第 634 行,在 get 返回 self._read_group(group) _read_group 中的文件“/home/denper/anaconda3/envs/py34/lib/python3.4/site-packages/pandas/io/pytables.py”,第 1272 行 返回 s.read(**kwargs) 文件“/home/denper/anaconda3/envs/py34/lib/python3.4/site-packages/pandas/io/pytables.py”,第 2788 行,已读 放置=items.get_indexer(blk_items)) make_block 中的文件“/home/denper/anaconda3/envs/py34/lib/python3.4/site-packages/pandas/core/internals.py”,第 2518 行 返回 klass(值,ndim=ndim,fastpath=fastpath,placement=placement) init 中的文件“/home/denper/anaconda3/envs/py34/lib/python3.4/site-packages/pandas/core/internals.py”,第 90 行 len(self.mgr_locs))) ValueError:传递的项目数错误 2,位置暗示 1 关闭剩余打开的文件:./test_issue.h5...done

另外,如果您在 Python3 中执行 generate_issue.py(保存 df1 或 df2),那么在 Python3 或 Python2 中执行 read_issue.py 都没有问题

【讨论】:

    猜你喜欢
    • 2020-04-10
    • 2020-03-19
    • 1970-01-01
    • 2017-05-06
    • 2020-10-11
    • 2021-02-02
    • 2020-01-29
    • 2021-04-09
    • 1970-01-01
    相关资源
    最近更新 更多