1

Is storing them in a CUBE type field and then getting euclidean distance, (using <->) the only way?

I can not save them as a list of floats, since i get: "can't adapt type numpyndarray"

Can not cast a type bytea to type cube, either.

Cube extension is installed, i can convert the list to type bytea using psycopg2, but nothing seems to work.

What am i missing?

I'm using postgresql 10.4, python 3, postgresapp 2.1.4 and postico

  • 2
    Please tag your DBMS (version included) – McNets May 22 '18 at 17:41
  • I can not save them as a list of floats, since i get: "can't adapt type numpyndarray" how are you trying to save them as floats? – Evan Carroll May 22 '18 at 23:04
  • @EvanCarroll well the list i have is indeed a list of floats, so i tried to save it specifying the table field type as doubleprecision[], but it gave me the "can´t adapt numpy..." error. – misterghost May 23 '18 at 16:58
0

I can not save them as a list of floats, since i get: "can't adapt type numpyndarray"

Quick google search shows numpyndarray as being SciPy's "N-dimensional array". This can hold what PostgreSQL calls a cube and vise-versa.

You assume that there is a translation layer that takes numpyndarray and converts the types to a cube for you. There likely isn't. You could provide such a layer, or extend your PostgreSQL connector/driver to provide that layer (see this for information on psycopg), but shy of that you'll have to go by way of text.

That means you'll be calling cube() or providing a text string and a cast to cube.

SELECT '(0,1,2,3,4,5)'::cube;
SELECT CAST ( '(0,1,2,3,4,5)' AS cube );

Your job is to convert numpyndarray to a textual representation like the above, and then to convert it back again.

Cubes can also be constructed as float[] which you may find easier if your DB layer supports that,

SELECT cube(ARRAY[0,1,2,3,4,5]::float[]); ## Array-constructor for float[]
SELECT cube('{1,2,3,4,5}'::float[]);      ## Text-constructor for float[]
  • Thanks a lot for your help...what do you mean when you say: "...numpyndarray as being SciPy's "N-dimensional array". This can hold what PostgreSQL calls a cube and vise-versa." ? Is my ndarray the same as having a cube? I have tried specifying my table field type as float[] and also as type cube and still get the "psycopg2.ProgrammingError: can't adapt type 'numpy.ndarray'" when trying to save my ndarray to either field. I already did: var = str(my_nd_array.tolist()) and then actually saved var into my table's text field. Will try your suggestions next. Does it sound good? Thanks again. – misterghost May 23 '18 at 18:58
  • No, PostgreSQL cube has a specific input (as text and binary) and a specific output (as text and binary). It's an implementation. Everything is free to implement the I/O to and from their library differently. There is no spec. Your problem is that your library isn't smart enough to serialize to PostgreSQL and your transport layer isn't smart enough either. – Evan Carroll May 23 '18 at 19:05
  • I can't tell where your problem is you'll have to find a PostgreSQL user to assist you, or make the 8 hour drive to Houston. =) Make sure your query is outputting in the right format -- check/enable your logs and see what your Python script is sending. – Evan Carroll May 23 '18 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.