Skip to content

Numpy strings cannot be loaded #88

Description

@moritzlayer

Hi, I just came across the following issue:

If you have numpy strings as keys of a dictionary and you save this dictionary, the h5py_wrapper raises the error

ValueError: malformed node or string: 
    <ast.Name object at 0x7fcfa1870970>

when trying to load the file. This can be reproduced using the following test (using pytest):

import pytest
import numpy as np
import h5py_wrapper as h5

from unittest import TestCase


def test_saving_and_consecutive_loading_of_numpy_string_keys(tmpdir):
    file = 'test.h5'
    keys = ['a', 'b', 'c']
    
    # this makes the keys numpy strings
    keys = np.atleast_1d(keys)
    output = {key: i for i, key in enumerate(keys)}

    # create temporary directory to save test file into
    tmp_test = tmpdir.mkdir('tmp_test')
    with tmp_test.as_cwd():
        h5.save(file, output)
        input = h5.load(file)
        TestCase().assertDictEqual(output, input)

The problem seems to be line 296 in wrapper.py where key_type is compared to ['str', 'unicode', 'string_'], but a numpy string leads to key_type = 'str_'.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions