[Numpy-discussion] Question about dtype

Nathaniel Smith njs at pobox.com
Wed Dec 10 18:46:35 EST 2014


On Wed, Dec 10, 2014 at 8:26 PM, Valentin Haenel <valentin at haenel.co> wrote:
> Hi,
>
> I am using numpy version 1.9.0 and Python 2.7.9 and have a question
> about the dtype:
>
> In [14]: np.dtype("<f8")
> Out[14]: dtype('float64')
>
> In [15]: np.dtype(u"<f8")
> Out[15]: dtype('float64')
>
> In [16]: np.dtype([("<f8", "<f8")])
> Out[16]: dtype([('<f8', '<f8')])
>
> So far so good. Now what happens if I use unicode?
>
> In [17]: np.dtype([(u"<f8", "<f8")])
> ---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent call
> last)
> <ipython-input-17-ce004acab7f5> in <module>()
> ----> 1 np.dtype([(u"<f8", "<f8")])
>
> TypeError: data type not understood

Yep, looks like a bug to me. (I guess this is particularly relevant
when __future__.unicode_literals is in effect.)

> Also, it really does need to be a tuple?
>
> In [18]: np.dtype([["<f8", "<f8"]])
> ---------------------------------------------------------------------------
> TypeError                                 Traceback (most recent call
> last)
> <ipython-input-18-c82761d7306d> in <module>()
> ----> 1 np.dtype([["<f8", "<f8"]])
>
> TypeError: data type not understood

Lists and tuples are both valid inputs to np.dtype, but they're
interpreted differently -- the problem here isn't that you used a
list, it's that if you use a list then numpy expects different
contents. See:
    http://docs.scipy.org/doc/numpy/user/basics.rec.html

-n

-- 
Nathaniel J. Smith
Postdoctoral researcher - Informatics - University of Edinburgh
http://vorpus.org



More information about the NumPy-Discussion mailing list