numpy.float64 is JSON serializable but numpy.float32 is not
Jan 20, 2016 · 211 words · 1 minute read
Update (2018-08-10)
I’ve written a newer, better post about how to serialize numpy.float32 to JSON. It also covers how to serialize other data types. I recommend reading that post instead.
Earlier this week I made a small modification which promptly broke the code I was working on. The change was so minor that it was very difficult to see why the changes would cause it to fail. As it turned out the change removed an implicit type conversion.
This led to the realization that the JSON module in Python can’t serialize NumPy 32-bit floats.
import numpy as np
import json
# This works fine
json.dumps({'pi': np.float64(3.1415)})
# so does this
json.dumps({'pi': 1.*np.float32(3.1415)})
# but this throws a TypeError
json.dumps({'pi': np.float32(3.1415)})
This can be important because by default NumPy aggregate functions (e.g. numpy.mean
) will return the same dtype
as the input dtype
(if the input is float). So if you want to calculate the mean of an array of 32-bit floats and encode the result as JSON you can expect this to fail. Multiplication implicitly converts the 32-bit float to 64-bit.
This seemingly esoteric distinction is actually pertinent to a number of applications. Images are often encoded with 32-bit color depth meaning each pixel is represented by 32-bits with 8-bits per channel (RGBA).