Python Forum
I am trying to change the value of an element in a record array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I am trying to change the value of an element in a record array
#1
[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
Reply
#2
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  RandomForest --ValueError: setting an array element with a sequence JaneTan 0 1,707 Sep-08-2021, 02:12 AM
Last Post: JaneTan
  TensorFlow get error - array with more than one element is ambiguous vokoyo 3 5,497 Nov-07-2019, 01:12 PM
Last Post: ThomasL
  How to add an element such as an average to a multi-dimensional array? xhughesey 6 3,917 Jan-06-2019, 10:47 PM
Last Post: xhughesey
  Convert element of list to integer(Two dimentional array) zorro_phu 3 4,625 Jun-12-2018, 04:49 AM
Last Post: zorro_phu
  ValueError: The truth value of an array with more than one element is ambiguous. Eliza5 1 14,279 Apr-02-2018, 12:03 AM
Last Post: scidam
  Compare 2 Csv data sets, identify record with latest date MJUk 11 6,095 Jan-06-2018, 09:23 PM
Last Post: MJUk
  How do you change specific elements in a char array of string? JoeB 4 6,473 Oct-31-2017, 09:50 AM
Last Post: JoeB

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020