Skip to content

Commit 3d439a6

Browse files
committed
updated read_hdf() --> create a new HDFStore only if arg filepath_or_buffer is not already an HDFStore
1 parent 849d350 commit 3d439a6

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

larray/inout/array.py

+34-25
Original file line numberDiff line numberDiff line change
@@ -496,31 +496,40 @@ def read_hdf(filepath_or_buffer, key, fill_value=np.nan, na=np.nan, sort_rows=Fa
496496

497497
key = _translate_key_hdf(key)
498498
res = None
499-
with pd.HDFStore(filepath_or_buffer, **kwargs) as store:
500-
pd_obj = store.get(key)
501-
attrs = store.get_storer(key).attrs
502-
# for backward compatibility but any object read from an hdf file should have an attribute 'type'
503-
_type = attrs.type if 'type' in dir(attrs) else 'LArray'
504-
if _type == 'LArray':
505-
res = df_aslarray(pd_obj, sort_rows=sort_rows, sort_columns=sort_columns, fill_value=fill_value, parse_header=False)
506-
elif _type == 'Axis':
507-
if name is None:
508-
name = str(pd_obj.name)
509-
if name == 'None':
510-
name = None
511-
res = Axis(labels=pd_obj.values, name=name)
512-
if 'wildcard' not in dir(attrs):
513-
raise ValueError("Attribute 'wildcard' is missing for axis {}".format(key))
514-
res._iswildcard = attrs['wildcard']
515-
elif _type == 'Group':
516-
if name is None:
517-
name = str(pd_obj.name)
518-
if name == 'None':
519-
name = None
520-
if 'axis_key' not in dir(attrs):
521-
raise ValueError("Attribute 'axis_key' is missing for group {}".format(key))
522-
axis = read_hdf(filepath_or_buffer, attrs['axis_key'])
523-
res = LGroup(key=pd_obj.values, name=name, axis=axis)
499+
if isinstance(filepath_or_buffer, pd.HDFStore):
500+
if not filepath_or_buffer.is_open:
501+
raise IOError('The HDFStore must be open for reading.')
502+
store = filepath_or_buffer
503+
close_store = False
504+
else:
505+
store = pd.HDFStore(filepath_or_buffer, mode='r')
506+
close_store = True
507+
pd_obj = store.get(key)
508+
attrs = store.get_storer(key).attrs
509+
# for backward compatibility but any object read from an hdf file should have an attribute 'type'
510+
_type = attrs.type if 'type' in dir(attrs) else 'LArray'
511+
if _type == 'LArray':
512+
res = df_aslarray(pd_obj, sort_rows=sort_rows, sort_columns=sort_columns, fill_value=fill_value, parse_header=False)
513+
elif _type == 'Axis':
514+
if name is None:
515+
name = str(pd_obj.name)
516+
if name == 'None':
517+
name = None
518+
res = Axis(labels=pd_obj.values, name=name)
519+
if 'wildcard' not in dir(attrs):
520+
raise ValueError("Attribute 'wildcard' is missing for axis {}".format(key))
521+
res._iswildcard = attrs['wildcard']
522+
elif _type == 'Group':
523+
if name is None:
524+
name = str(pd_obj.name)
525+
if name == 'None':
526+
name = None
527+
if 'axis_key' not in dir(attrs):
528+
raise ValueError("Attribute 'axis_key' is missing for group {}".format(key))
529+
axis = read_hdf(filepath_or_buffer, attrs['axis_key'])
530+
res = LGroup(key=pd_obj.values, name=name, axis=axis)
531+
if close_store:
532+
store.close()
524533
return res
525534

526535
@deprecate_kwarg('nb_index', 'nb_axes', arg_converter=lambda x: x + 1)

0 commit comments

Comments
 (0)