我有一个像这样的numpy数组:
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)]
我想把它分成2个numpy数组:
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]
为了向您提供更多详细信息,这个numpy数组最初是一个词典,我将它转换为numpy数组,您可以找到以下代码:
def convertarray(dictionary):
names=['id','data']
formats=['datetime64[ms]','f8']
dtype=dict(names=names, formats=formats)
result=np.array(dictionary.items(),dtype)
return result
答案 0 :(得分:1)
如果你只是一个带有dtype=object
的香草阵列,我认为你最好的办法就是通过在几个列表推理中迭代旧数组来构造新数组:
将numpy导入为np 来自numpy import nan 导入日期时间
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] for x in A])
values = np.array([x[1] for x in A])
print(times)
print(values)
话虽如此,使用记录数组可能稍微清晰一点:
import numpy as np
from 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'])
答案 1 :(得分:-1)
您可能想要切片数据。为该维度插入:
将选择该维度的所有元素。
B = A[:, 0]
C = A[:, 1]