2010-03-29

sqlalchemy ProgrammingError can't adapt type numpy.float64 - Python troubleshooting

Are you getting a

sqlalchemy.exc.ProgrammingError: (ProgrammingError) can't adapt type 'numpy.float64'

???

It's because psycopg (psycopg2) - which is used inherently by SQL Alchemy but if you're just dealing directly with alchemy you might not know this - has no idea how to make the numpy.core.numerictypes.float64 (complete type path: another thing they don't tell you) a string for SQL querying.

Simple solution, add:

# solution by Jorge Orpinel found at rehalcon.blogspotcom
import numpy
from psycopg2.extensions import register_adapter, AsIs
def addapt_numpy_float64(numpy_float64):
return AsIs(numpy_float64)
register_adapter(numpy.float64, addapt_numpy_float64)

somewhere in the program.

For a more complete reference of the problem see http://initd.org/psycopg/docs/advanced.html#adapting-new-python-types-to-sql-syntax

3 comments:

  1. Could this solution be simplified by not defining the addapt_numpy_float64 function and just using AsIs instead?

    e.g.
    register_adapter(numpy.float64, AsIs)

    ReplyDelete
  2. This was super helpful. I had exactly this problem (with numpy.float32 type). I have a Flask app that connects to a postgres db. I put this in my app's __init__.py, and now everything works perfectly! Thank you!

    ReplyDelete
  3. @Genevine I'm glad this post from 6 years ago helped you! Good luck out there (:

    ReplyDelete

Simple server load test with cron and ab (Linux)

Load testing "refers to the practice of modeling the expected usage of a software program by simulating multiple users accessing the p...