user9098935
user9098935

Reputation:

How to have a numpy array with mixed types?

I would like to create a numpy array with mixed types. The other SO questions that I found either create an object based array or an nested array.

Both I do not want.

How would the syntax look like to have a numpy array with one str and two int columns?

This is my present code:

import numpy as np

b = np.empty((0, 3), )
b = np.insert(b, b.shape[0], [[1, 2, 3]], axis=0)
b = np.insert(b, b.shape[0], [[1, 2, 3]], axis=0)

print(b)
print("---")

a = np.empty((0, 3), dtype='S4, int, int')
a = np.insert(a, a.shape[0], ("a", 2, 3), axis=0)
a = np.insert(a, a.shape[0], ("a", 2, 3), axis=0)

print(a)

The output:

[[1. 2. 3.]
 [1. 2. 3.]]
---
[[(b'a', 2, 3) (b'a', 2, 3) (b'a', 2, 3)]
 [(b'a', 2, 3) (b'a', 2, 3) (b'a', 2, 3)]]

EDIT:

And what I need for the array a is:

[["a" 2 3]
 ["a" 2 3]]

Upvotes: 1

Views: 4907

Answers (1)

hpaulj
hpaulj

Reputation: 231665

Your second array is close, though I'd do it with indexing rather than insert (which is slower):

In [431]: a = np.zeros(3, dtype='S4, int, int')
In [432]: a[0] = ('a', 2, 3)
In [433]: a[1] = 1
In [434]: a
Out[434]: 
array([(b'a', 2, 3), (b'1', 1, 1), (b'', 0, 0)],
      dtype=[('f0', 'S4'), ('f1', '<i8'), ('f2', '<i8')])

A list of tuples is also a good way of constructing such an array:

In [436]: a = np.array([('a',2,3),('b',4,5)], dtype='S4, int, int')
In [437]: a
Out[437]: 
array([(b'a', 2, 3), (b'b', 4, 5)],
      dtype=[('f0', 'S4'), ('f1', '<i8'), ('f2', '<i8')])

Note that the shape is 1d (n,), with 3 fields. The fields don't count as a dimension.

Fields are accessed by name, not 'column' number:

In [438]: a['f1']
Out[438]: array([2, 4])

You made a (2,3) array, and filled each 'row' with the same thing. That's why you have repeats, while I don't.

With a unicode string dtype (default for Py3):

In [439]: a = np.array([('a',2,3),('b',4,5)], dtype='U4, int, int')
In [440]: a
Out[440]: 
array([('a', 2, 3), ('b', 4, 5)],
      dtype=[('f0', '<U4'), ('f1', '<i8'), ('f2', '<i8')])
In [441]: print(a)
[('a', 2, 3) ('b', 4, 5)]

Upvotes: 2

Related Questions