6

I want to get a dataframe as hdf in memory. The code below results in "AttributeError: '_io.BytesIO' object has no attribute 'put'". I am using python 3.5 and pandas 0.17

import pandas as pd
import numpy as np
import io

df = pd.DataFrame(np.arange(8).reshape(-1, 2), columns=['a', 'b'])
buf = io.BytesIO()
df.to_hdf(buf, 'some_key')

Update: As UpSampler pointed out "path_or_buf" cannot be an io stream (which I find confusing since buf usually can be an io stream, see to_csv). Other than writing to disk and reading it back in, can I get a dataframe as hdf in memory?

user2133814
  • 2,431
  • 1
  • 24
  • 34

2 Answers2

2

Your first argument to df.to_hdf() has to be a "path (string) or HDFStore object" not an io stream. Documentation: http://pandas.pydata.org/pandas-docs/version/0.17.0/generated/pandas.DataFrame.to_hdf.html

UpSampler
  • 399
  • 2
  • 6
  • Ahh, didn't see that. to_csv has a similar argument path_or_buf where buf can be an io stream, which is why I got confused. – user2133814 Jan 06 '17 at 14:25
  • CSV is a a bulit-in module and HDF is external. I don't know what pandas is using for HDF file access, could be pytables or pyhdf... – UpSampler Jan 07 '17 at 01:58
-1

just try this

df = pd.DataFrame(np.arange(8).reshape(-1, 2), columns=['a', 'b'])
df.to_hdf(path_or_buf='path\to\your\file')

refer pandas.DataFrame.to_hdf

Shijo
  • 9,313
  • 3
  • 19
  • 31