I'm trying to create a NumPy array/matrix (Nx3) with mixed data types (string, integer, integer). But when I'm appending this matrix by adding some data, I get an error: TypeError: invalid type promotion. Please, can anybody help me to solve this problem?
When I create an array with the sample data, NumPy casts all columns in the matrix to the one 'S' data type. And I can't specify data type for an array, because when i do this res = np.array(["TEXT", 1, 1], dtype='S, i4, i4') - I get an error: TypeError: expected a readable buffer object
templates.py
import numpy as np
from pprint import pprint
test_array = np.zeros((0, 3), dtype='S, i4, i4')
pprint(test_array)
test_array = np.append(test_array, [["TEXT", 1, 1]], axis=0)
pprint(test_array)
print("Array example:")
res = np.array(["TEXT", 1, 1])
pprint(res)
Output:
array([], shape=(0L, 3L),
dtype=[('f0', 'S'), ('f1', '<i4'), ('f2', '<i4')])
Array example:
array(['TEXT', '1', '1'], dtype='|S4')
Error:
Traceback (most recent call last):
File "templates.py", line 5, in <module>
test_array = np.append(test_array, [["TEXT", 1, 1]], axis=0)
File "libsite-packages
umpylibfunction_base.py", line 3543, in append
return concatenate((arr, values), axis=axis)
TypeError: invalid type promotion
See Question&Answers more detail:
os