Skip to content

Commit 55a3e96

Browse files
committed
fixed odd issue when reading axis objects from HDF files on Python2
1 parent 592173a commit 55a3e96

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

larray/inout/hdf.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,14 @@ def read_hdf(filepath_or_buffer, key, fill_value=nan, na=nan, sort_rows=False, s
9090
name = None
9191
labels = pd_obj.values
9292
if 'dtype_kind' in attrs and attrs['dtype_kind'] == 'U':
93-
labels = np.char.decode(labels, 'utf-8')
93+
# this check is there because there are cases where dtype_kind is 'U' but pandas returns
94+
# an array with object dtype containing bytes instead of a string array, and in that case
95+
# np.char.decode does not work
96+
# this is at least the case for Python2 + Pandas 0.24.2 combination
97+
if labels.dtype.kind == 'O':
98+
labels = np.array([l.decode('utf-8') for l in labels], dtype='U')
99+
else:
100+
labels = np.char.decode(labels, 'utf-8')
94101
res = Axis(labels=labels, name=name)
95102
res._iswildcard = attrs['wildcard']
96103
elif _type == 'Group':

0 commit comments

Comments
 (0)