网络上下载了vstoxx_data_31032014.h5 文件,因为代码报一些错误,把文件后缀改为了vstoxx_data_31032014.hdf5,
h5=pd.HDFStore('D:/机器学习算法/Python与金融应用/Python/sources/vstoxx_data_31032014.hdf5')
futures_data=h5['futres_data']
options_data=ht['options_data']
h5.close()
用另一种方法读取hdf文件:
import h5py
hdfFile=h5py.File('D:/机器学习算法/Python与金融应用/Python/sources/vstoxx_data_31032014.hdf5','r')
futures_data=hdfFile.get('futures_data')
options_data=hdfFile.get('options_data')
hdfFile.close()
但是这里我遇到一个问题就是:
print(futures_data.shape()
Error是:
去了解了一下HDF5文件数据,发现里面是有group和datasets属性,
所以futures_data=hdfFile.get(‘futures_data’) 这里得到的是group?
后来用另外一种读法还是不行:
import h5py
hdfFile=h5py.File('D:/机器学习算法/Python与金融应用/Python/sources/vstoxx_data_31032014.hdf5','r')
futures_data_group=hdfFile['futures_data']
futures_data=futures_data_group[:]
options_data_group=hdfFile['options_data']
options_data=options_data_group[:]
hdfFile.close()
print(futures_data.shape())
报错:
AttributeError: 'slice' object has no attribute 'encode'
‘futures_data’ group下面有8个datasets:
<HDF5 group "/futures_data" (8 members)> futures_data /futures_data
<HDF5 dataset "axis0": shape (6,), type "|S9"> /futures_data/axis0
<HDF5 dataset "axis1": shape (8,), type "<i8"> /futures_data/axis1
<HDF5 dataset "block0_items": shape (2,), type "|S8"> /futures_data/block0_items
<HDF5 dataset "block0_values": shape (8, 2), type "<i8"> /futures_data/block0_values
<HDF5 dataset "block1_items": shape (2,), type "|S5"> /futures_data/block1_items
<HDF5 dataset "block1_values": shape (8, 2), type "<f8"> /futures_data/block1_values
<HDF5 dataset "block2_items": shape (2,), type "|S9"> /futures_data/block2_items
<HDF5 dataset "block2_values": shape (8, 2), type "<i8"> /futures_data/block2_values
用了一个笨方法来读取group 下面datasets:
# Columns: [b'DATE', b'EXP_YEAR', b'EXP_MONTH', b'PRICE', b'MATURITY', b'TTM']
import pandas as pd
import h5py
from pandas import to_datetime
hdfFile=h5py.File('D:/机器学习算法/Python与金融应用/Python/sources/vstoxx_data_31032014.hdf5','r')
futures_data=pd