
Why do the follow expressions give different dtype?
np.array([1, 2, 3], dtype=str)
array(['1', '2', '3'], dtype='|S1')
np.array(np.array([1, 2, 3]), dtype=str)
array(['1', '2', '3'], dtype='|S8')

On Thu, May 20, 2010 at 4:04 PM, Keith Goodman kwgoodman@gmail.com wrote:
Why do the follow expressions give different dtype?
np.array([1, 2, 3], dtype=str)
array(['1', '2', '3'], dtype='|S1')
np.array(np.array([1, 2, 3]), dtype=str)
array(['1', '2', '3'], dtype='|S8')
you're on a 64bit machine?
S8 is the same size as the float
np.array([8]).itemsize
4
np.array(np.array([1, 2, 3]), dtype=str)
array(['1', '2', '3'], dtype='|S4')
np.array([8]).view(dtype='S4')
array(['\x08'], dtype='|S4')
np.array([8]).view(dtype='S1')
array(['\x08', '', '', ''], dtype='|S1')
But I don't know whether this is a desired feature, numpy might reuse the existing buffer (?)
Josef
NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

On Thu, May 20, 2010 at 4:19 PM, josef.pktd@gmail.com wrote:
On Thu, May 20, 2010 at 4:04 PM, Keith Goodman kwgoodman@gmail.com wrote:
Why do the follow expressions give different dtype?
np.array([1, 2, 3], dtype=str)
array(['1', '2', '3'], dtype='|S1')
np.array(np.array([1, 2, 3]), dtype=str)
array(['1', '2', '3'], dtype='|S8')
you're on a 64bit machine?
S8 is the same size as the float
not float, it should be int, here is float on my Win32:
np.array(np.array([1., 2, 3]), dtype=str)
array(['1.0', '2.0', '3.0'], dtype='|S8')
np.array([8.]).itemsize
8
np.array([8]).itemsize
4
np.array(np.array([1, 2, 3]), dtype=str)
array(['1', '2', '3'], dtype='|S4')
np.array([8]).view(dtype='S4')
array(['\x08'], dtype='|S4')
np.array([8]).view(dtype='S1')
array(['\x08', '', '', ''], dtype='|S1')
But I don't know whether this is a desired feature, numpy might reuse the existing buffer (?)
Josef
NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

On Thu, May 20, 2010 at 1:19 PM, josef.pktd@gmail.com wrote:
On Thu, May 20, 2010 at 4:04 PM, Keith Goodman kwgoodman@gmail.com wrote:
Why do the follow expressions give different dtype?
np.array([1, 2, 3], dtype=str)
array(['1', '2', '3'], dtype='|S1')
np.array(np.array([1, 2, 3]), dtype=str)
array(['1', '2', '3'], dtype='|S8')
you're on a 64bit machine?
S8 is the same size as the float
np.array([8]).itemsize
4
np.array(np.array([1, 2, 3]), dtype=str)
array(['1', '2', '3'], dtype='|S4')
np.array([8]).view(dtype='S4')
array(['\x08'], dtype='|S4')
np.array([8]).view(dtype='S1')
array(['\x08', '', '', ''], dtype='|S1')
But I don't know whether this is a desired feature, numpy might reuse the existing buffer (?)
Yes, I'm on a 64-bit machine.
That's what I thought so I tried this:
a = np.array([1, 2, 3]) type(a[0])
<type 'numpy.int64'>
np.array([a[0], a[1], a[2]], dtype=str)
array(['1', '2', '3'], dtype='|S1')
But it gives '|S1' too. I guess I'm lost.

On Thu, May 20, 2010 at 4:28 PM, Keith Goodman kwgoodman@gmail.com wrote:
On Thu, May 20, 2010 at 1:19 PM, josef.pktd@gmail.com wrote:
On Thu, May 20, 2010 at 4:04 PM, Keith Goodman kwgoodman@gmail.com wrote:
Why do the follow expressions give different dtype?
np.array([1, 2, 3], dtype=str)
array(['1', '2', '3'], dtype='|S1')
np.array(np.array([1, 2, 3]), dtype=str)
array(['1', '2', '3'], dtype='|S8')
you're on a 64bit machine?
S8 is the same size as the float
np.array([8]).itemsize
4
np.array(np.array([1, 2, 3]), dtype=str)
array(['1', '2', '3'], dtype='|S4')
np.array([8]).view(dtype='S4')
array(['\x08'], dtype='|S4')
np.array([8]).view(dtype='S1')
array(['\x08', '', '', ''], dtype='|S1')
But I don't know whether this is a desired feature, numpy might reuse the existing buffer (?)
Yes, I'm on a 64-bit machine.
That's what I thought so I tried this:
a = np.array([1, 2, 3]) type(a[0])
<type 'numpy.int64'>
np.array([a[0], a[1], a[2]], dtype=str)
array(['1', '2', '3'], dtype='|S1')
But it gives '|S1' too. I guess I'm lost.
for sure it doesn't look very consistent, special treatment of 0-dim ?
np.array(a[0], dtype=str)
array('1', dtype='|S1')
np.array(a[:1], dtype=str)
array(['1'], dtype='|S4')
a[:1].shape
(1,)
a[0].shape
()
Josef
NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

Thu, 20 May 2010 13:04:11 -0700, Keith Goodman wrote:
Why do the follow expressions give different dtype?
np.array([1, 2, 3], dtype=str)
array(['1', '2', '3'], dtype='|S1')
np.array(np.array([1, 2, 3]), dtype=str)
array(['1', '2', '3'], dtype='|S8')
Scalars seem to be handled specially. Anyway, automatic determination of the string size is a bit dangerous to rely on with non-strings in the array:
np.array([np.array(12345)], dtype=str)
array(['1234'], dtype='|S4')
When I looked at this the last time, it wasn't completely obvious how to make this to do something more sensible.
participants (3)
-
josef.pktd@gmail.com
-
Keith Goodman
-
Pauli Virtanen