Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy: is it possible to preserve the dtype of columns when using column_stack

Tags:

python

numpy

When I use column_stack to concatenate NumPy arrays, the dtype gets converted:

a = numpy.array([1., 2., 3.], dtype=numpy.float64)
b = numpy.array([1, 2, 3], dtype=numpy.int64)
print numpy.column_stack((a, b)).dtype
>>> float64

Is there a way to preserve the dtype of the individual columns?

like image 693
fetteelke Avatar asked Sep 24 '14 13:09

fetteelke


People also ask

What is NumPy Column_stack () used for?

column_stack() function is used to stack 1-D arrays as columns into a 2-D array.It takes a sequence of 1-D arrays and stack them as columns to make a single 2-D array. 2-D arrays are stacked as-is, just like with hstack function.

Can NumPy store different data types?

Data Types in NumPyNumPy has some extra data types, and refer to data types with one character, like i for integers, u for unsigned integers etc. Below is a list of all data types in NumPy and the characters used to represent them.

Can NumPy arrays efficiently store data?

NumPy uses much less memory to store data The NumPy arrays takes significantly less amount of memory as compared to python lists. It also provides a mechanism of specifying the data types of the contents, which allows further optimisation of the code.


2 Answers

You can stack two arrays with numpy.lib.recfunctions method and preserve the type with it:

>>> from  numpy.lib.recfunctions import append_fields

>>> a = numpy.rec.array(a, dtype=[('a', numpy.float64)])
>>> new_a = append_fields(a, 'b', b, usemask=False, dtypes=[numpy.int64])
>>> new_a
array([(1.0, 1), (2.0, 2), (3.0, 3)], 
      dtype=[('a', '<f8'), ('b', '<i8')])

>>> new_a['a']
array([ 1.,  2.,  3.])

>>> new_a['b']
array([1, 2, 3])
like image 194
Dalek Avatar answered Oct 10 '22 03:10

Dalek


My arrays got converted into string (S18) when I column_stack'ed em.

I used astype(desired dtype) to convert them back to what they were after the stacking.

http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.astype.html

example:

new_array= old_array.astype(float64) 
like image 20
Labibah Avatar answered Oct 10 '22 04:10

Labibah