定义numpy记录数据类型时,“数据类型不可理解”错误

时间:2013-12-03 13:41:22

标签: python django numpy

我正在尝试创建一个numpy记录数组来从游标加载数据。

我在为此记录定义data_type时收到错误,“数据类型未被理解”。

从我看到的例子中,我不清楚我在这里做错了什么。

python代码:

@login_required
def resource(request, resource_id):
    cnxn = pyodbc.connect(SOLAR_SECURED_CONNECTION)
    cursor = cnxn.cursor()

    sqlstr = """select r.name as resource_name, rm.mobile_id_id, fd.value, pd.update_time, rt.capacity
                    from asset_monitor_resource r, asset_monitor_formulateddata fd, asset_monitor_resourcetype rt,
                         asset_monitor_parseddata pd, asset_monitor_resourcemapping rm
                    where r.id = rm.resource_id_id and
                          r.id = fd.resource_id_id and
                          fd.rawdata_id_Id = pd.rawdata_id_id and
                          r.resource_type_id_id = rt.id and
                          r.id = ?
                    order by pd.update_time desc"""

    cursor.execute(sqlstr, resource_id)

    data_type = np.dtype([('resource_name', np.str, 100),
                          ('mobile_id_id', np.int),
                          ('value', np.float),
                          ('update_time', np.datetime_data),
                          ('capacity', np.int)])

    narray = np.fromiter(cursor.fetchall(), count=-1,
                         dtype=data_type)

    r_rows = cursor.fetchall()

    dateplot(narray, resource_id)
    image_file = "tempfig"+str(resource_id)+".png"

    return render_to_response('resource.html', {'data': r_rows, 'resource_id': resource_id, 'image_file': image_file}, context_instance=RequestContext(request))

回溯

[Tue Dec 03 08:17:24 2013] [error] Internal Server Error: /user/resource/97/
[Tue Dec 03 08:17:24 2013] [error] Traceback (most recent call last):
[Tue Dec 03 08:17:24 2013] [error]   File "C:\\Python27\\Lib\\site-packages\\django\\core\\handlers\\base.py", line 115, in get_response
[Tue Dec 03 08:17:24 2013] [error]     response = callback(request, *callback_args, **callback_kwargs)
[Tue Dec 03 08:17:24 2013] [error]   File "C:\\Python27\\Lib\\site-packages\\django\\contrib\\auth\\decorators.py", line 25, in _wrapped_view
[Tue Dec 03 08:17:24 2013] [error]     return view_func(request, *args, **kwargs)
[Tue Dec 03 08:17:24 2013] [error]   File "C:\\dev\\solar_secured\\asset_monitor\\views.py", line 310, in resource
[Tue Dec 03 08:17:24 2013] [error]     ('capacity', np.int)])
[Tue Dec 03 08:17:24 2013] [error] TypeError: data type not understood

1 个答案:

答案 0 :(得分:3)

没有dtype np.datetime_data,它是一个函数:

  

datetime_data(dtype)

     Return (unit, numerator, denominator, events) from a datetime dtype

使用正确的数据类型np.datetime64,例如:

>>> np.dtype([('resource_name', np.str, 100),
...           ('mobile_id_id', np.int),
...           ('value', np.float),
...           ('update_time', np.datetime64),
...           ('capacity', np.int)])
dtype([('resource_name', 'S100'), ('mobile_id_id', '<i8'), ('value', '<f8'), ('update_time', '<M8'), ('capacity', '<i8')])