简体   繁体   English

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

[英]'data type not understood' error when defining numpy record data type

I am attempting to create a numpy record array to load data from a cursor. 我试图创建一个numpy记录数组来从游标加载数据。

I am receiving an error, 'data type not understood' when I define the data_type for this record. 我在为此记录定义data_type时收到错误“数据类型未被理解”。

From the examples I have seen, I am not clear what I am doing wrong here. 从我看到的例子中,我不清楚我在这里做错了什么。

python code: 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))

traceback 追溯

[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

There is no dtype np.datetime_data , its a function: 没有np.datetime_data ,它是一个函数:

datetime_data(dtype)

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

Use proper data type, np.datetime64 for example: 使用正确的数据类型,例如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')])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM