diff --git a/fact/VERSION b/fact/VERSION index 9b40aa6..9028ec6 100644 --- a/fact/VERSION +++ b/fact/VERSION @@ -1 +1 @@ -0.10.4 +0.10.5 diff --git a/fact/io.py b/fact/io.py index 3f99159..0b9d3e7 100644 --- a/fact/io.py +++ b/fact/io.py @@ -224,7 +224,7 @@ def check_extension(file_path, allowed_extensions=allowed_extensions): raise IOError('Allowed formats: {}'.format(allowed_extensions)) -def to_h5py(filename, df, key='data', mode='w', dtypes=None, index=True, **kwargs): +def to_h5py(filename, df, key='data', mode='a', dtypes=None, index=True, **kwargs): ''' Write pandas dataframe to h5py style hdf5 file @@ -253,7 +253,7 @@ def to_h5py(filename, df, key='data', mode='w', dtypes=None, index=True, **kwarg array = change_recarray_dtype(array, dtypes) with h5py.File(filename, mode=mode) as f: - if mode == 'w': + if key not in f: initialize_h5py(f, array.dtype, key=key, **kwargs) append_to_h5py(f, array, key=key) diff --git a/tests/test_io.py b/tests/test_io.py index 016bf89..c034d91 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -92,6 +92,32 @@ def test_to_h5py_append(): assert all(df_read[col] == df_written[col]) +def test_to_h5py_append_second_group(): + from fact.io import to_h5py, read_h5py + + df1 = pd.DataFrame({ + 'x': np.random.normal(size=50), + 'N': np.random.randint(0, 10, dtype='uint8') + }) + df2 = pd.DataFrame({ + 'x': np.random.normal(size=50), + 'N': np.random.randint(0, 10, dtype='uint8') + }) + + with tempfile.NamedTemporaryFile() as f: + to_h5py(f.name, df1, key='g1', index=False) + to_h5py(f.name, df2, key='g2', index=False) + + df_g1 = read_h5py(f.name, key='g1') + df_g2 = read_h5py(f.name, key='g2') + + for col in df_g1.columns: + assert all(df_g1[col] == df1[col]) + + for col in df_g2.columns: + assert all(df_g2[col] == df2[col]) + + def test_write_data_csv(): from fact.io import write_data