使用pytables模块将pandas dataframe写入hdf5

时间:2016-12-09 20:25:47

标签: python pandas hdf5 pytables

在下面的示例中,我尝试以import java.awt.Component; import java.awt.EventQueue; import java.awt.image.BufferedImage; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class Example { public void createAndShowGUI() { JButton button = new JButton("Text"); button.setSize(button.getPreferredSize()); JLabel label = new JLabel(); label.setIcon(new ImageIcon(snapshot(button))); JPanel contentPane = new JPanel(); contentPane.add(label); JFrame frame = new JFrame(); frame.setContentPane(contentPane); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationByPlatform(true); frame.setVisible(true); } private BufferedImage snapshot(Component component) { BufferedImage image = new BufferedImage(component.getWidth(), component.getHeight(), BufferedImage.TYPE_INT_RGB); component.paint(image.createGraphics()); return image; } public static void main(String[] args) { EventQueue.invokeLater(() -> new Example().createAndShowGUI()); } } 文件格式存储多索引pandas.DataFrame

h5

如果我尝试将上述数据帧('abc')存储到存储中,我会收到错误,如下所示

import pandas as pd
import numpy as np
store = pd.HDFStore('c:/temp/example.h5')
idx = pd.MultiIndex.from_arrays([['first']*3,['one','two','three']], names = ['outer','inner'])
abc = pd.DataFrame(np.random.randint(1,100,9).reshape(-1,3), index = idx, columns = list('abc'))

之前我没有收到此错误,也许可能与pytable的版本更新有关。我目前正在使用3.2.2版。关于什么可以作为以h5格式存储数据帧的替代解决方案的任何指针?

1 个答案:

答案 0 :(得分:0)

你有什么理由不能使用to_hdf function吗?

abc.to_hdf('c:/temp/example.h5')