These are chat archives for numpy/numpy

20th
Sep 2016
Francesc Elies
@FrancescElies
Sep 20 2016 14:27

Hi,
I tried to serialize numpy.int64, I googled it and found many results describing the same issue, but I did not understand if this was bug or not

import json
import numpy as np
print('numpy', np.__version__)
# numpy 1.11.0
arr_float64 = np.array([1., 2., 3.])
arr_int64 = np.array([1, 2, 3])
json.dumps(list(arr_float64))
json.dumps(list(arr_int64))

In the script above the second dumps fails, could someone help me to understand this issue? thanks in advance

Eugene Pakhomov
@p-himik
Sep 20 2016 14:52
In [16]: import inspect as ins

In [17]: ins.getmro(np.int64)
Out[17]:
(numpy.int64,
 numpy.signedinteger,
 numpy.integer,
 numpy.number,
 numpy.generic,
 object)

In [18]: ins.getmro(np.float64)
Out[18]:
(numpy.float64,
 numpy.floating,
 numpy.inexact,
 numpy.number,
 numpy.generic,
 float,
 object)
For float64 you can see float as one of its bases.
I don't think it's a defect. And if you really need to JSON'ify a Numpy array, it's better to either call .tolist() on it or to implement a JSON encoder - depending on what is simpler/more performant in your particular case.