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 "lib\site-packages\numpy\lib\function_base.py", line 3543, in append
return concatenate((arr, values), axis=axis)
TypeError: invalid type promotion
Introduction to Data Types In lists, the types of elements can be mixed. One index of a list can contain an integer, another can contain a string. This is not the case for arrays. In an array, each element must be of the same type.
No, we cannot store multiple datatype in an Array, we can store similar datatype only in an Array.
In general numpy arrays can have more than one dimension. One way to create such array is to start with a 1-dimensional array and use the numpy reshape() function that rearranges elements of that array into a new shape.
Character code 'B' Alias on this platform (Linux x86_64) numpy. uint8: 8-bit unsigned integer (0 to 255). Most often this is used for arrays representing images, with the 3 color channels having small integer values (0 to 255). Follow this answer to receive notifications. answered Jul 15, 2021 at 2:41.
Your problem is in the data. Try this:
res = np.array(("TEXT", 1, 1), dtype='|S4, i4, i4')
or
res = np.array([("TEXT", 1, 1), ("XXX", 2, 2)], dtype='|S4, i4, i4')
The data has to be a tuple or a list of tuples. Not quite evident form the error message, is it?
Also, please note that the length of the text field has to be specified for the text data to really be saved. If you want to save the text as objects (only references in the array, then:
res = np.array([("TEXT", 1, 1), ("XXX", 2, 2)], dtype='object, i4, i4')
This is often quite useful, as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With