[Numpy-discussion] astype None

josef.pktd at gmail.com josef.pktd at gmail.com
Thu May 20 22:36:04 EDT 2010


On Thu, May 20, 2010 at 9:00 PM, Keith Goodman <kwgoodman at gmail.com> wrote:
> While automating some unit tests for my labeled array class, larry, I
> assumed that
>
> np.array([1, 2], dtype=dtype)
>
> would give the same result as
>
> np.array([1, 2]).astype(dtype)
>
> But it doesn't for dtype=None:
>
>>> np.array([1, 2, 3], dtype=None)
>   array([1, 2, 3])
>>> np.array([1, 2, 3]).astype(None)
>   array([ 1.,  2.,  3.])
>
> I prefer the behavior of array where dtype=None is a no-op.

Since nobody who knows this answered, I try my explanation

It's all in the docs

astype(None)  cast to a specified type

here the dtype is "None"
None is by default float_
>>> np.dtype(None)
dtype('float64')

np.array([1, 2, 3], dtype=None)
np.asarray([1, 2, 3], dtype=None)

here dtype is a keyword argument where None is not a dtype but
triggers the default, which is:
dtype : data-type, optional
By default, the data-type is inferred from the input data.

Shall we start a list of inconsistent looking corner cases ?)

Josef



> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



More information about the NumPy-Discussion mailing list