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 noop.
On Thu, May 20, 2010 at 9:00 PM, Keith Goodman <kwgoodman@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 noop.
np.dtype(None)
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_ 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 : datatype, optional By default, the datatype is inferred from the input data. Shall we start a list of inconsistent looking corner cases ?) Josef
_______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
On Thu, May 20, 2010 at 7:36 PM, <josef.pktd@gmail.com> wrote:
On Thu, May 20, 2010 at 9:00 PM, Keith Goodman <kwgoodman@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 noop.
Since nobody who knows this answered, I try my explanation
It's all in the docs
astype(None) cast to a specified type
np.dtype(None)
here the dtype is "None" None is by default float_ 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 : datatype, optional By default, the datatype is inferred from the input data.
Shall we start a list of inconsistent looking corner cases ?)
It's easy to find this sort of stuff with short nose tests. Here's a quick hacked example: import numpy as np from numpy.testing import assert_equal def test_astype_dtype(): "array.astype test" dtypes = [float, int, str, bool, complex, object, None] seqs = ([0, 1], [1.0, 2.0]) msg1 = 'arrays failed on dtype %s and sequence %s' msg2 = 'dtype are different when dtype=%s and seq=%s' for dtype in dtypes: for seq in seqs: ar1 = np.array(list(seq), dtype=dtype) # array does dtype ar2 = np.array(list(seq)).astype(dtype) # astype does dtype yield assert_equal, ar1, ar2, msg1 % (dtype, seq) yield assert_equal, ar1.dtype, ar2.dtype, msg2 % (dtype, seq) The output is =================== FAIL: array.astype test  <...> AssertionError: Items are not equal: dtype are different when dtype=None and seq=[0, 1] ACTUAL: dtype('int64') DESIRED: dtype('float64')
participants (2)

josef.pktd＠gmail.com

Keith Goodman