python - Split an numpy array into two numpy arrays -


i have numpy array this:

a=[(datetime.datetime(2016, 6, 8, 12, 37, 27, 826000), 3.0)    (datetime.datetime(2016, 6, 8, 12, 37, 27, 827000), nan)    (datetime.datetime(2016, 6, 8, 12, 37, 27, 832000), nan)    (datetime.datetime(2016, 6, 8, 12, 37, 27, 833000), nan)    (datetime.datetime(2016, 6, 8, 12, 37, 27, 837000), 3.0)    (datetime.datetime(2016, 6, 8, 12, 37, 27, 837000), 35.0)] 

and want split 2 numpy arrays:

b=[(datetime.datetime(2016, 6, 8, 12, 37, 27, 826000),   (datetime.datetime(2016, 6, 8, 12, 37, 27, 827000),   (datetime.datetime(2016, 6, 8, 12, 37, 27, 832000),   (datetime.datetime(2016, 6, 8, 12, 37, 27, 833000),   (datetime.datetime(2016, 6, 8, 12, 37, 27, 837000),    (datetime.datetime(2016, 6, 8, 12, 37, 27, 837000)]  c=[3.0,nan,nan,nan,3.0,35.0] 

to give more details numpy array @ first dictionnary , i've convert numpy array, can find code below:

def convertarray(dictionary):     names=['id','data']     formats=['datetime64[ms]','f8']     dtype=dict(names=names, formats=formats)     result=np.array(dictionary.items(),dtype)     return result 

if have vanilla array dtype=object, think best recourse construct new arrays iterating on old 1 in couple list-comprehensions:

import numpy np numpy import nan import datetime

a=np.array([(datetime.datetime(2016, 6, 8, 12, 37, 27, 826000), 3.0),    (datetime.datetime(2016, 6, 8, 12, 37, 27, 827000), nan),    (datetime.datetime(2016, 6, 8, 12, 37, 27, 832000), nan),    (datetime.datetime(2016, 6, 8, 12, 37, 27, 833000), nan),    (datetime.datetime(2016, 6, 8, 12, 37, 27, 837000), 3.0),    (datetime.datetime(2016, 6, 8, 12, 37, 27, 837000), 35.0)])  print(a.dtype)  times = np.array([x[0] x in a]) values = np.array([x[1] x in a])  print(times) print(values) 

with said, might cleaner use record array:

import numpy np numpy import nan import datetime  a=np.array([(datetime.datetime(2016, 6, 8, 12, 37, 27, 826000), 3.0),    (datetime.datetime(2016, 6, 8, 12, 37, 27, 827000), nan),    (datetime.datetime(2016, 6, 8, 12, 37, 27, 832000), nan),    (datetime.datetime(2016, 6, 8, 12, 37, 27, 833000), nan),    (datetime.datetime(2016, 6, 8, 12, 37, 27, 837000), 3.0),    (datetime.datetime(2016, 6, 8, 12, 37, 27, 837000), 35.0)],    dtype=[('time', object), ('value', float)])  print(a.dtype)  print(a['time']) print(a['value']) 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -