Skip to content

Commit d9c83da

Browse files
committed
fix larray-project#79 : included metadata when saving/loading an array to/from an HDF file.
1 parent 3c8caa2 commit d9c83da

3 files changed

Lines changed: 7 additions & 2 deletions

File tree

‎larray/core/array.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6113,6 +6113,7 @@ def to_hdf(self, filepath, key):
61136113
with LHDFStore(filepath) as store:
61146114
store.put(key, self.to_frame())
61156115
store.get_storer(key).attrs.type = 'Array'
6116+
self.meta.to_hdf(store, key)
61166117

61176118
@deprecate_kwarg('sheet_name', 'sheet')
61186119
def to_excel(self, filepath=None, sheet=None, position='A1', overwrite_file=False, clear_sheet=False,

‎larray/inout/hdf.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,12 @@ def read_hdf(filepath_or_buffer, key, fill_value=np.nan, na=np.nan, sort_rows=Fa
9494
attrs = store.get_storer(key).attrs
9595
# for backward compatibility but any object read from an hdf file should have an attribute 'type'
9696
_type = attrs.type if 'type' in attrs else 'Array'
97+
_meta = attrs.metadata if 'metadata' in attrs else None
9798
if _type == 'Array':
9899
res = df_aslarray(pd_obj, sort_rows=sort_rows, sort_columns=sort_columns, fill_value=fill_value,
99100
parse_header=False)
101+
if _meta is not None:
102+
res.meta = _meta
100103
elif _type == 'Axis':
101104
if name is None:
102105
name = str(pd_obj.name)

‎larray/tests/test_array.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2652,8 +2652,8 @@ def test_expand(meta):
26522652
c = Axis('c=c1,c2')
26532653
assert ndtest([a, b], meta=meta).expand([a, c, b]).meta == meta
26542654

2655-
def test_hdf_roundtrip(tmpdir):
2656-
a = ndtest((2, 3))
2655+
def test_hdf_roundtrip(tmpdir, meta):
2656+
a = ndtest((2, 3), meta=meta)
26572657
fpath = tmp_path(tmpdir, 'test.h5')
26582658
a.to_hdf(fpath, 'a')
26592659
res = read_hdf(fpath, 'a')
@@ -2662,6 +2662,7 @@ def test_hdf_roundtrip(tmpdir):
26622662
assert a.shape == (2, 3)
26632663
assert a.axes.names == ['a', 'b']
26642664
assert_array_equal(res, a)
2665+
assert res.meta == a.meta
26652666

26662667
# issue 72: int-like strings should not be parsed (should round-trip correctly)
26672668
fpath = tmp_path(tmpdir, 'issue72.h5')

0 commit comments

Comments
 (0)