python - pyodbc SQL Query to Numpy array typeerror: bytes-like object required -
i've been trying pull data sql database using pyodbc , want place numpy.array. found difficulty in inputting multiple data-type np.fromiter() argument.
import pyodbc od import numpy np con = od.connect('dsn=dbase; uid=user; pwd=pass') cursor = con.cursor() sqlcommand = ( """ select [item no_] ,sum ([quantity]) totqty ,sum ([discount amount]) discamount ,sum ([cost amount]) costamount ,[date] ,sum ([net amount]) netamount ,sum ([vat amount]) vatamount ,sum ([refund qty_]) refundqty database [date] between ('2015-12-01 00:00:00.000') , ('2015-12-31 00:00:00.000') , [area no_] = '123' group rollup([date],[item no_]); """) cursor.execute(sqlcommand) results = cursor.fetchall() results_as_list = [i[0] in results] array = np.fromiter(results_as_list, dtype="str, float, float, datetime64,float,float,float") print(array[:5,:]) and error
typeerror: bytes-like object required, not 'str'
you attempting pass 1 iterable, first column of query, multiple dtypes. numpy.iter() takes 1 object , type. consider passing each column of query resultset one-dimensional iterable. string (variable-length) need specify length , date include suffix if [d] date or time [s].
cursor.execute(sqlcommand) results = cursor.fetchall() array = [np.fromiter([i[0] in results], dtype="|s50"), np.fromiter([i[1] in results], dtype="float"), np.fromiter([i[2] in results], dtype="float"), np.fromiter([i[3] in results], dtype="float"), np.fromiter([i[4] in results], dtype="datetime64[s]"), np.fromiter([i[5] in results], dtype="float"), np.fromiter([i[6] in results], dtype="float"), np.fromiter([i[7] in results], dtype="float")] alternatively, created array of matrices:
array = np.array([np.matrix([i[0] in results], dtype="str"), np.matrix([i[1] in results], dtype="float"), np.matrix([i[2] in results], dtype="float"), np.matrix([i[3] in results], dtype="float"), np.matrix([i[4] in results], dtype="str"), np.matrix([i[5] in results], dtype="float"), np.matrix([i[6] in results], dtype="float"), np.matrix([i[7] in results], dtype="float")])
Comments
Post a Comment