Numpy array for SQL table

I am trying to store multiple pickled Numpy arrays in a SQL database. Numpy arrays represent 3D and shapes (Name (text), Data (floats), Date (int)

.

I am currently doing it like this (for an array arr

containing data and names

and dates

as references containing the actual names and dates that match arr

)

name_size, ~, date_size = arr.shape
for i in range(name_size):
    for j in range(date_size):
        insert_into_db(name[i], date[j], arr[i,:,j)

      

However, this is very slow. I'm wondering if there isn't a more efficient way just by looking at the object arr

as a whole.

For example, first paste the references to names

and dates

into the database, and then somehow just copy the values ​​in arr

right into all at once (where they are ordered and smoothed correctly with the reference to Name

and Date

that we just pasted.

+3


source to share


1 answer


If your database cannot contain native numpy arrays, you can use the dumps

or methods tostring

.

dumps

collects data into an object bytes

in both Python 3.x and str

Python 2.x objects , which can then be stored in the database as a sequence of strings or raw bytes. The trick is that the pickle format can change between python or numpy versions, so another numpy or python version won't necessarily be able to read it (although the numpy developers try to keep the pickle reader as backward compatible as possible):

testarr = np.arange(20)
data = testarr.dumps()

      

Which gives you (in python 3.x, it's different from python 2.x):

b'\x80\x02cnumpy.core.multiarray\n_reconstruct\nq\x00cnumpy\nndarray\nq\x01K\x00\x85q\x02c_codecs\nencode\nq\x03X\x01\x00\x00\x00bq\x04X\x06\x00\x00\x00latin1q\x05\x86q\x06Rq\x07\x87q\x08Rq\t(K\x01K\x14\x85q\ncnumpy\ndtype\nq\x0bX\x02\x00\x00\x00i8q\x0cK\x00K\x01\x87q\rRq\x0e(K\x03X\x01\x00\x00\x00<q\x0fNNNJ\xff\xff\xff\xffJ\xff\xff\xff\xffK\x00tq\x10b\x89h\x03X\xa0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00\x07\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\t\x00\x00\x00\x00\x00\x00\x00\n\x00\x00\x00\x00\x00\x00\x00\x0b\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x00\x00\x00\x00\x00\r\x00\x00\x00\x00\x00\x00\x00\x0e\x00\x00\x00\x00\x00\x00\x00\x0f\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00\x11\x00\x00\x00\x00\x00\x00\x00\x12\x00\x00\x00\x00\x00\x00\x00\x13\x00\x00\x00\x00\x00\x00\x00q\x11h\x05\x86q\x12Rq\x13tq\x14b.'

      



tostring

works in a similar way to converting an array to a string format. The advantage is that it has to be the same for python and numpy versions, but has the disadvantage that it does not preserve dimensions, so you will need to preserve the dimensions (and whether there will be an array C

or Fortran

order) in the database in order to correctly reconstruct the array (if it's not always the same):

testarr = np.arange(20)
data = testarr.tostring()

      

What you give (it will be the same in Python 2.x and 3.x, except that in Python 3.x it will be a type bytes

, and in python 2.x it will be a str

type):

b'\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00\x07\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\t\x00\x00\x00\x00\x00\x00\x00\n\x00\x00\x00\x00\x00\x00\x00\x0b\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x00\x00\x00\x00\x00\r\x00\x00\x00\x00\x00\x00\x00\x0e\x00\x00\x00\x00\x00\x00\x00\x0f\x00\x00\x00\x00\x00\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00\x11\x00\x00\x00\x00\x00\x00\x00\x12\x00\x00\x00\x00\x00\x00\x00\x13\x00\x00\x00\x00\x00\x00\x00'

      

+2


source







All Articles