[Numpy-discussion] dtype argument description for np.array

Kirill Balunov kirillbalunov at gmail.com
Fri Feb 9 05:30:49 EST 2018


Currently in docstring the description of dtype argument for np.array says
this:

dtype : data-type, optional
>     The desired data-type for the array.  If not given, then the type will
>     be determined as the minimum type required to hold the objects in the
>     sequence.  This argument can only be used to 'upcast' the array.  For
>     downcasting, use the .astype(t) method.
>

But I found this description somewhat misleading for integer types. Is this
generally true that "the type will
be determined as the minimum type required to hold the objects in the
sequence."?

As I always thought for integer arrays first `np.int_` is assumed and if it
is not enough the bigger dtype s are used, and finally falling to `object`
dtype. This question comes from this discussion:
https://github.com/numba/numba/issues/2729.

Also somewhat (un)related (but I was always curious), why `float` type was
chosen as a default for dtype?

>>> np.array([]).dtype
dtype('float64')
>>> np.ones([]).dtype
dtype('float64')

Why np.int_ was not chosen and will this change be a good idea (in general,
without taking into account backward compatibility :-))? There is some
discussion here: https://github.com/numpy/numpy/issues/10405.

-----------------

p.s.: For some constructors the signature looks as follows (in IPython
console):

>>> np.zeros?
Docstring:
zeros(shape, dtype=float, order='C')

Return a new array of given shape and type, filled with zeros.


>>> np.empty?
Docstring:
empty(shape, dtype=float, order='C')

Return a new array of given shape and type, without initializing entries.


But for `ones` the dtype is none instead of float and looks different:

>>> np.ones?
Signature: np.ones(shape, dtype=None, order='C')
Docstring:
Return a new array of given shape and type, filled with ones.

-----------------

With kind regards,
-gdg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20180209/c7d984d1/attachment.html>


More information about the NumPy-Discussion mailing list