Skip to content

Handle encoding for numpy array scalars #18

@rldotai

Description

@rldotai

The json_tricks library will serialize instances of numpy.ndarray but if you pass it an array scalar (e.g., a single element of an array, which have datatypes like numpy.int64, numpy.float64, etc.)

For example:

>>> z = np.array([1,2,3]) >>> json_tricks.dumps(z) '{"dtype": "int64", "shape": [3], "__ndarray__": [1, 2, 3]}' >>> json_tricks.dumps(z[0]) TypeError: Object of type <class 'numpy.int64'> could not be encoded by TricksEncoder using encoders [<function json_date_time_encode at 0x7f38028fea60>, <function class_instance_encode at 0x7f38028febf8>, <function json_complex_encode at 0x7f38028fec80>, <function numpy_encode at 0x7f3802410400>]

My present workaround is to ensure that all array scalars are converted to numpy arrays before attempting to serialize (json_tricks.dumps(np.array(z[0])) in the above) but it would be easier if this was handled automatically.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions