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
Could this solution be simplified by not defining the addapt_numpy_float64 function and just using AsIs instead?
ReplyDeletee.g.
register_adapter(numpy.float64, AsIs)
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@Genevine I'm glad this post from 6 years ago helped you! Good luck out there (:
ReplyDelete