Pandas HDF5 追加时间序列失败
Pandas HDF5 append time series fails
查看 pandas HDF5 可用性 (http://pandas.pydata.org/pandas-docs/stable/io.html#io-hdf5) 的文档,给出的示例会引发错误:
import pandas as pd
import numpy as np
store = pd.HDFStore('store.h5')
np.random.seed(1234)
index = pd.date_range('1/1/2000', periods=8)
df = pd.DataFrame(np.random.randn(8, 3), index=index)
store['df'] = df
df1 = df[0:4]
df2 = df[4:]
store.append('df', df1)
store.append('df', df2)
Traceback (most recent call last):
File "C:\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2885, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-225-ef7f2e059c6a>", line 1, in <module>
store.append('df', df1)
File "C:\Anaconda3\lib\site-packages\pandas\io\pytables.py", line 919, in append
**kwargs)
File "C:\Anaconda3\lib\site-packages\pandas\io\pytables.py", line 1252, in _write_to_group
raise ValueError('Can only append to Tables')
ValueError: Can only append to Tables
这里有什么变化吗?还是我做错了什么?
您需要启用 append
默认情况下 table 格式的商店,方法是在开头设置以下选项,因为您的商店目前的行为类似于 DF
:
pd.set_option('io.hdf.default_format','table')
查看 pandas HDF5 可用性 (http://pandas.pydata.org/pandas-docs/stable/io.html#io-hdf5) 的文档,给出的示例会引发错误:
import pandas as pd
import numpy as np
store = pd.HDFStore('store.h5')
np.random.seed(1234)
index = pd.date_range('1/1/2000', periods=8)
df = pd.DataFrame(np.random.randn(8, 3), index=index)
store['df'] = df
df1 = df[0:4]
df2 = df[4:]
store.append('df', df1)
store.append('df', df2)
Traceback (most recent call last):
File "C:\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2885, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-225-ef7f2e059c6a>", line 1, in <module>
store.append('df', df1)
File "C:\Anaconda3\lib\site-packages\pandas\io\pytables.py", line 919, in append
**kwargs)
File "C:\Anaconda3\lib\site-packages\pandas\io\pytables.py", line 1252, in _write_to_group
raise ValueError('Can only append to Tables')
ValueError: Can only append to Tables
这里有什么变化吗?还是我做错了什么?
您需要启用 append
默认情况下 table 格式的商店,方法是在开头设置以下选项,因为您的商店目前的行为类似于 DF
:
pd.set_option('io.hdf.default_format','table')