question
I'm having trouble identifying numpy.int64
objects, in order to convert them to base python int for json serialisation. isinstance
usually works but does not in the following example, and I would love to understand why this is.
>>> x
0
>>> type(x)
<class 'numpy.int64'>
>>> import numpy
>>> isinstance(x, numpy.int64)
False
context
x in the above comes from my application, generated by to_dict
on a pandas dataframe. Various dataframes are used to generate the result, hence why I can't just use pandas to_json
instead.
taking hints from How to identify numpy types in python?, I've actually managed to sucessfully detect these items (which sometimes aren't numpy objects at all), using the following:
>>> (isinstance(x, (pd.np.ndarray, pd.np.generic)) and
>>> pd.np.issubdtype(x, pd.np.dtype('int64')))
True
but I would appreciate very much if someone could explain why the first option doesn't work, so that I can be confident enough to deploy this into our production system. Using simplejson
and a custom JSONDecoder
class which uses isinstance(obj, pd.np.int64)
has worked for months, but it has suddenly stopped working with the example above.
pickle.dumps(x)
gives b'\x80\x03cnumpy.core.multiarray\nscalar\nq\x00cnumpy\ndtype\nq\x01X\x02\x00\x00\x00i8q\x02K\x00K\x01\x87q\x03Rq\x04(K\x03X\x01\x00\x00\x00<q\x05NNNJ\xff\xff\xff\xffJ\xff\xff\xff\xffK\x00tq\x06bC\x08\x00\x00\x00\x00\x00\x00\x00\x00q\x07\x86q\x08Rq\t.'
interestingly, pickling the object seems to fix the issue.
>>> isinstance(pickle.loads(pickle.dumps(x)), pd.np.int64)
True