Skip to content

Commit

Permalink
Merge pull request #63 from fact-project/append_group
Browse files Browse the repository at this point in the history
Append group
  • Loading branch information
maxnoe authored May 18, 2017
2 parents 188088d + 9833b03 commit fc658cd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion fact/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.10.4
0.10.5
4 changes: 2 additions & 2 deletions fact/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit fc658cd

Please sign in to comment.