2016-09-22 19 views
2

我有一個名爲weather.h5的h5商店。我的默認Python環境是3.5.2。當我嘗試閱讀這家商店時,我收到TypeError: Already tz-aware, use tz_convert to convert使用熊貓閱讀h5文件時出現「tz-aware」錯誤,python 3(但不是2)

我試過pd.read_hdf('weather.h5','weather_history')pd.io.pytables.HDFStore('weather.h5')['weather_history],但是無論如何我都會得到錯誤。

我可以在Python 2.7環境中打開h5。這是Python 3/pandas中的錯誤嗎?

+0

請注意,我可以使用'weather_store = pd.io.pytables.HDFStore('weather.h5')加載h5。 ',但是當我嘗試使用'weather_store ['weather_history']'得到'TypeError'時引發表。 –

回答

0

我有同樣的問題。我正在使用Anaconda Python:3.4.5和2.7.3。兩者都使用熊貓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:在Python2

import pandas as pd 
from pandas import HDFStore 

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

print(df) 

運行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 (most recent call last): File "read_issue.py", line 5, in df = store['df'] File "/home/denper/anaconda3/envs/py34/lib/python3.4/site-packages/pandas/io/pytables.py", line 417, in getitem return self.get(key) File "/home/denper/anaconda3/envs/py34/lib/python3.4/site-packages/pandas/io/pytables.py", line 634, in get return self._read_group(group) File "/home/denper/anaconda3/envs/py34/lib/python3.4/site-packages/pandas/io/pytables.py", line 1272, in _read_group return s.read(**kwargs) File "/home/denper/anaconda3/envs/py34/lib/python3.4/site-packages/pandas/io/pytables.py", line 2779, in read ax = self.read_index('axis%d' % i) File "/home/denper/anaconda3/envs/py34/lib/python3.4/site-packages/pandas/io/pytables.py", line 2367, in read_index _, index = self.read_index_node(getattr(self.group, key)) File "/home/denper/anaconda3/envs/py34/lib/python3.4/site-packages/pandas/io/pytables.py", line 2492, in read_index_node _unconvert_index(data, kind, encoding=self.encoding), **kwargs) File "/home/denper/anaconda3/envs/py34/lib/python3.4/site-packages/pandas/indexes/base.py", line 153, in new result = DatetimeIndex(data, copy=copy, name=name, **kwargs) File "/home/denper/anaconda3/envs/py34/lib/python3.4/site-packages/pandas/util/decorators.py", line 91, in wrapper return func(*args, **kwargs) File "/home/denper/anaconda3/envs/py34/lib/python3.4/site-packages/pandas/tseries/index.py", line 321, in new raise TypeError("Already tz-aware, use tz_convert " TypeError: Already tz-aware, use tz_convert to convert. Closing remaining open files:./test_issue.h5...done

所以,有一個與指數的問題。但是,如果在保存generate.py DF2(日期時間作爲列,而不是作爲索引),然後在Python3 read_issue.py產生不同的錯誤:

Traceback (most recent call last): File "read_issue.py", line 5, in df = store['/df'] File "/home/denper/anaconda3/envs/py34/lib/python3.4/site-packages/pandas/io/pytables.py", line 417, in getitem return self.get(key) File "/home/denper/anaconda3/envs/py34/lib/python3.4/site-packages/pandas/io/pytables.py", line 634, in get return self._read_group(group) File "/home/denper/anaconda3/envs/py34/lib/python3.4/site-packages/pandas/io/pytables.py", line 1272, in _read_group return s.read(**kwargs) File "/home/denper/anaconda3/envs/py34/lib/python3.4/site-packages/pandas/io/pytables.py", line 2788, in read placement=items.get_indexer(blk_items)) File "/home/denper/anaconda3/envs/py34/lib/python3.4/site-packages/pandas/core/internals.py", line 2518, in make_block return klass(values, ndim=ndim, fastpath=fastpath, placement=placement) File "/home/denper/anaconda3/envs/py34/lib/python3.4/site-packages/pandas/core/internals.py", line 90, in init len(self.mgr_locs))) ValueError: Wrong number of items passed 2, placement implies 1 Closing remaining open files:./test_issue.h5...done

另外,如果你在Python3執行generate_issue.py (保存df1或df2),那麼在Python3或Python2中執行read_issue.py沒有問題。