Skip to content

Instantly share code, notes, and snippets.

@RobbieClarken
Created February 23, 2016 06:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobbieClarken/9ea7ceaaa3765f536d95 to your computer and use it in GitHub Desktop.
Save RobbieClarken/9ea7ceaaa3765f536d95 to your computer and use it in GitHub Desktop.
with h5py.File('data.hdf5', 'w') as file:
dataset = file.create_dataset('dataset', data=df.values)
dataset.attrs['index'] = np.array(df.index.tolist(), dtype='S')
dataset.attrs['columns'] = np.array(df.columns.tolist(), dtype='S')
def make_index(raw):
index = raw.astype('U')
if index.ndim > 1:
return pd.MultiIndex.from_tuples(index.tolist())
else:
return pd.Index(index)
with h5py.File('data.hdf5') as file:
dataset = file['dataset']
index = make_index(dataset.attrs['index'])
columns = make_index(dataset.attrs['columns'])
df = pd.DataFrame(data=dataset[...], index=index, columns=columns)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment