无法将元素添加到Numpy数组

时间:2016-12-11 02:49:19

标签: python numpy

我试图将一个元素添加到Numpy数组中。我正在做这样的事情(插入代码片段,所以如果我错过了一些关键信息,请告诉我 - 我会添加它)。我希望有人帮助解释我在np.append函数中做错了什么。

====================================

#Data type declaration for each element of array.
dt = [
    ('device', 'int64'),
    ('battery', 'u2'),
    ('version', 'f4'),
    ('ChargeStatus', 'u2'),
    ('temperature', 'u2'),
    ('time', 'datetime64'),
    ]

#dataArr is the final array and data is the individual element. 

dataArr = np.zeros(0, dtype=dt)
data = np.empty(1, dtype=dt)

<snip - code that reads a file, parses and writes into "data"> 

#The following line of code does NOT write any data into dataArr
np.append (dataArr, data)

#However, if i change the above line as below, dataArr gets the populated array i'm looking for. 

dataArr = np.append (dataArr, data)

1 个答案:

答案 0 :(得分:0)

dataArr = np.append(dataArr,data)

是正确的方法。