[Numpy-discussion] Question about dtype

Eelco Hoogendoorn hoogendoorn.eelco at gmail.com
Sat Dec 13 05:07:42 EST 2014


This is a general problem in trying to use JSON to send arbitrary python
objects. Its not made for that purpose, JSON itself only supports a very
limited grammar (only one sequence type for instance, as you noticed), so
in general you will need to specify your own encoding/decoding for more
complex objects you want to send over JSON.

In the case of an object dtype, dtypestr = str(dtype) gives you a nice
JSONable string representation, which you can convert back into a dtype
using np.dtype(eval(dtypestr))

On Sat, Dec 13, 2014 at 9:25 AM, Sebastian <sebix at sebix.at> wrote:
>
> Hi,
>
> I'll just comment on the creation of your dtype:
>
> > dt = [("<f8", "<f8")]
>
> You are creating a dtype with one field called '<f8' and with type '<f8':
>
> >>> dt = [("<f8", "<f8")]
> >>> dty = np.dtype(dt)
> >>> dty.names
>
> ('<f8',)
>
> What you may want are two fields with type '<f8' and without fieldname:
>
> >>> dt = [("<f8", "<f8")]
> >>> dty = np.dtype(('<f8,<f8'))
> >>> dty.names
>
> ('f0', 'f1')
> >>> dty.descr
>
> [('f0', '<f8'), ('f1', '<f8')]
>
> I can't help you with the json-module and what it's doing there. As the
> output is unequal to the input, I suspect JSON to be misbehaving here.
> If you need to store the dtype as strings, not as binary pickle, you can
> use pickle.dumps and pickle.loads
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20141213/14db2d95/attachment.html>


More information about the NumPy-Discussion mailing list