Python Forum

Full Version: I am trying to change the value of an element in a record array
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
[inline]import numpy as np
person_data_def = [('name', 'm8'),('height', 'f8'),('weight', 'f8'),('age', 'i8')]
people_array = np.zeros(4, dtype=person_data_def)
print(people_array)
people_array[0] = ('delta', 73, 300, 38)
print(people_array)[/inline]

Output:
[(0, 0., 0., 0) (0, 0., 0., 0) (0, 0., 0., 0) (0, 0., 0., 0)]
Error:
Traceback (most recent call last): File "C:/Users/user/PycharmProjects/test/test2.py", line 5, in <module> people_array[0] = ('delta', 73, 300, 38) ValueError: Could not convert object to NumPy timedelta
dtype is set to timedelta and then you want to pass string? Quite obviously numpy doesn't allow that, otherwise - what is the point of dtypes.

Maybe following can help:

>>> person_data_def = [('name', 'm8'),('height', 'f8'),('weight', 'f8'),('age', 'i8')]                                                       
>>> people_array = np.zeros(4, dtype=person_data_def)      # you create 4-row array of zeros
>>> people_array
array([(0, 0., 0., 0), (0, 0., 0., 0), (0, 0., 0., 0), (0, 0., 0., 0)],
      dtype=[('name', '<m8'), ('height', '<f8'), ('weight', '<f8'), ('age', '<i8')])                                                                                     
>>> np.dtype(people_array[0][0])                            # first element of first row                                                                                                            
dtype('<m8') 
>>> people_array[0][0] = 5                                  # accepts integer                                                                                                                  
>>> people_array                                                                                                                             
array([(5, 0., 0., 0), (0, 0., 0., 0), (0, 0., 0., 0), (0, 0., 0., 0)],
      dtype=[('name', '<m8'), ('height', '<f8'), ('weight', '<f8'), ('age', '<i8')])
>>> delta = np.array(50, dtype='<m8')                                                                                                       
>>> delta                                                                                                                                   
array(50, dtype=timedelta64)
>>> people_array[1][0] = delta                               # accepts timedelta type                                                                                                            
>>> people_array                                                                                                                            
array([( 5, 0., 0., 0), (50, 0., 0., 0), ( 0, 0., 0., 0), ( 0, 0., 0., 0)],
      dtype=[('name', '<m8'), ('height', '<f8'), ('weight', '<f8'), ('age', '<i8')])
>>> another_delta = np.array(60, dtype=np.timedelta64)                                                                                      
>>> people_array[2] = (another_delta, 1, 2, 3)                # accepts tuple of correct datatypes                                                                                            
>>> people_array                                                                                                                             
array([( 5, 0., 0., 0), (50, 0., 0., 0), (60, 1., 2., 3), ( 0, 0., 0., 0)],
      dtype=[('name', '<m8'), ('height', '<f8'), ('weight', '<f8'), ('age', '<i8')])
>>> people_array[0].dtype.names                                # get names                                                                                                            
('name', 'height', 'weight', 'age')
>>> people_array[0].dtype.fields                                                                                                            
mappingproxy({'name': (dtype('<m8'), 0),
              'height': (dtype('float64'), 8),
              'weight': (dtype('float64'), 16),
              'age': (dtype('int64'), 24)})
>>> people_array['name']                                                                                                                    
array([ 5, 50, 60,  0], dtype=timedelta64)                      # name column values