3
3
import warnings
4
4
5
5
import numpy as np
6
+ import pandas as pd
6
7
from pandas import HDFStore
7
8
8
9
from larray .core .array import Array
12
13
from larray .core .metadata import Metadata
13
14
from larray .util .misc import LHDFStore
14
15
from larray .inout .session import register_file_handler
15
- from larray .inout .common import FileHandler
16
+ from larray .inout .common import FileHandler , _supported_typenames , _supported_scalars_types
16
17
from larray .inout .pandas import df_asarray
17
18
from larray .example import get_example_filepath
18
19
19
20
21
+ # for backward compatibility (larray < 0.29) but any object read from an hdf file should have
22
+ # an attribute 'type'
23
+ def _get_type_from_attrs (attrs ):
24
+ return attrs .type if 'type' in attrs else 'Array'
25
+
26
+
20
27
def read_hdf (filepath_or_buffer , key , fill_value = nan , na = nan , sort_rows = False , sort_columns = False ,
21
28
name = None , ** kwargs ):
22
- r"""Reads an axis or group or array named key from a HDF5 file in filepath (path+name)
29
+ r"""Reads a scalar or an axis or group or array named key from a HDF5 file in filepath (path+name)
23
30
24
31
Parameters
25
32
----------
26
33
filepath_or_buffer : str or pandas.HDFStore
27
34
Path and name where the HDF5 file is stored or a HDFStore object.
28
35
key : str or Group
29
- Name of the array.
36
+ Name of the scalar or axis or group or array.
30
37
fill_value : scalar or Array, optional
31
38
Value used to fill cells corresponding to label combinations which are not present in the input.
32
39
Defaults to NaN.
@@ -70,11 +77,14 @@ def read_hdf(filepath_or_buffer, key, fill_value=nan, na=nan, sort_rows=False, s
70
77
key = _translate_group_key_hdf (key )
71
78
res = None
72
79
with LHDFStore (filepath_or_buffer ) as store :
73
- pd_obj = store .get (key )
80
+ try :
81
+ pd_obj = store .get (key )
82
+ except KeyError :
83
+ filepath = filepath_or_buffer if isinstance (filepath_or_buffer , HDFStore ) else store .filename
84
+ raise KeyError ('No item with name {} has been found in file {}' .format (key , filepath ))
74
85
attrs = store .get_storer (key ).attrs
75
86
writer = attrs .writer if 'writer' in attrs else None
76
- # for backward compatibility but any object read from an hdf file should have an attribute 'type'
77
- _type = attrs .type if 'type' in attrs else 'Array'
87
+ _type = _get_type_from_attrs (attrs )
78
88
_meta = attrs .metadata if 'metadata' in attrs else None
79
89
if _type == 'Array' :
80
90
# cartesian product is not necessary if the array was written by LArray
@@ -110,6 +120,10 @@ def read_hdf(filepath_or_buffer, key, fill_value=nan, na=nan, sort_rows=False, s
110
120
key = np .char .decode (key , 'utf-8' )
111
121
axis = read_hdf (filepath_or_buffer , attrs ['axis_key' ])
112
122
res = LGroup (key = key , name = name , axis = axis )
123
+ elif _type in _supported_typenames :
124
+ res = pd_obj .values
125
+ assert len (res ) == 1
126
+ res = res [0 ]
113
127
return res
114
128
115
129
@@ -126,36 +140,37 @@ def _open_for_write(self):
126
140
127
141
def list_items (self ):
128
142
keys = [key .strip ('/' ) for key in self .handle .keys ()]
143
+ items = [(key , _get_type_from_attrs (self .handle .get_storer (key ).attrs )) for key in keys if '/' not in key ]
144
+ # ---- for backward compatibility (LArray < 0.33) ----
129
145
# axes
130
- items = [(key .split ('/' )[- 1 ], 'Axis ' ) for key in keys if '__axes__' in key ]
146
+ items + = [(key .split ('/' )[- 1 ], 'Axis_Backward_Comp ' ) for key in keys if '__axes__' in key ]
131
147
# groups
132
- items += [(key .split ('/' )[- 1 ], 'Group' ) for key in keys if '__groups__' in key ]
133
- # arrays
134
- items += [(key , 'Array' ) for key in keys if '/' not in key ]
148
+ items += [(key .split ('/' )[- 1 ], 'Group_Backward_Comp' ) for key in keys if '__groups__' in key ]
135
149
return items
136
150
137
- def _read_item (self , key , type , * args , ** kwargs ):
138
- if type == 'Array' :
151
+ def _read_item (self , key , typename , * args , ** kwargs ):
152
+ if typename in _supported_typenames :
139
153
hdf_key = '/' + key
140
- elif type == 'Axis' :
154
+ # ---- for backward compatibility (LArray < 0.33) ----
155
+ elif typename == 'Axis_Backward_Comp' :
141
156
hdf_key = '__axes__/' + key
142
- elif type == 'Group ' :
157
+ elif typename == 'Group_Backward_Comp ' :
143
158
hdf_key = '__groups__/' + key
144
159
else :
145
160
raise TypeError ()
146
161
return read_hdf (self .handle , hdf_key , * args , ** kwargs )
147
162
148
163
def _dump_item (self , key , value , * args , ** kwargs ):
149
- if isinstance (value , Array ):
150
- hdf_key = '/' + key
151
- value .to_hdf (self .handle , hdf_key , * args , ** kwargs )
152
- elif isinstance (value , Axis ):
153
- hdf_key = '__axes__/' + key
164
+ hdf_key = '/' + key
165
+ if isinstance (value , (Array , Axis )):
154
166
value .to_hdf (self .handle , hdf_key , * args , ** kwargs )
155
167
elif isinstance (value , Group ):
156
- hdf_key = '__groups__/' + key
157
- hdf_axis_key = '__axes__/' + value .axis .name
168
+ hdf_axis_key = '/' + value .axis .name
158
169
value .to_hdf (self .handle , hdf_key , hdf_axis_key , * args , ** kwargs )
170
+ elif isinstance (value , _supported_scalars_types ):
171
+ s = pd .Series (data = value )
172
+ self .handle .put (hdf_key , s )
173
+ self .handle .get_storer (hdf_key ).attrs .type = type (value ).__name__
159
174
else :
160
175
raise TypeError ()
161
176
0 commit comments