Comrade Che
Comrade Che

Reputation: 732

Why converting numpy.ndarray to custom data type using numpy.ndarray.astype multiplies my data?

Executing this code:

import numpy as np

py_list = [2013, 8, 0.6552562894775783]
custom_type = np.dtype([
                        ('YEAR',np.uint16),
                        ('DOY', np.uint16),
                        ('REF',np.float16)
                        ])

NumPy_array = np.array(py_list)
NumPy_array_converted = NumPy_array.astype(custom_type)

print 'custom_type is:'
print custom_type
print '---------------------------------------------'
print 'py_list is:'
print py_list
print '---------------------------------------------'
print 'NumPy_array is:'
print NumPy_array
print '---------------------------------------------'
print 'NumPy_array converted to custom_type is:'
print NumPy_array_converted
print '---------------------------------------------'

Prints:

custom_type is:
[('YEAR', '<u2'), ('DOY', '<u2'), ('REF', '<f2')]
---------------------------------------------
py_list is:
[2013, 8, 0.6552562894775783]
---------------------------------------------
NumPy_array is:
[  2.01300000e+03   8.00000000e+00   6.55256289e-01]
---------------------------------------------
NumPy_array converted to custom_type is:
[(2013, 2013, 2013.0) (8, 8, 8.0) (0, 0, 0.6552734375)]
---------------------------------------------

1) The question is why after convertion to custom data type my data is tripled NumPy_array_converted in comparison with not converted numpy array NumPy_array?

2) How to change custom_type to get not trippled array?

Upvotes: 1

Views: 839

Answers (1)

hpaulj
hpaulj

Reputation: 231385

np.array([tuple(py_list)], custom_type)

produces

array([(2013, 8, 0.6552734375)], 
  dtype=[('YEAR', '<u2'), ('DOY', '<u2'), ('REF', '<f2')])

Data for structured arrays are supposed to a list of tuples (orjust one tuple). Notice how the values are displayed. [(...)].

There may be a way of doing this with astype, but it is tricky.

Also,notice what NumPy_array is - 3 floats, while py_list is 2 ints and a float. And custom_type wants to convert those 2 ints to 'u2'. They aren't compatible.

Upvotes: 1

Related Questions