Third Party Integration Issues

The numpy package has its own numeric datatypes that extend from Python’s numeric types, but contain some behaviors that in some cases make them impossible to reconcile with some of SQLAlchemy’s behaviors, as well as in some cases with those of the underlying DBAPI driver in use.

Two errors which can occur are ProgrammingError: can't adapt type 'numpy.int64' on a backend such as psycopg2, and ArgumentError: SQL expression object expected, got object of type <class 'numpy.bool_'> instead; in more recent versions of SQLAlchemy this may be ArgumentError: SQL expression for WHERE/HAVING role expected, got True.

In the first case, the issue is due to psycopg2 not having an appropriate lookup entry for the int64 datatype such that it is not accepted directly by queries. This may be illustrated from code based on the following:

  1. import numpy
  2. class A(Base):
  3. __tablename__ = "a"
  4. id = Column(Integer, primary_key=True)
  5. data = Column(Integer)
  6. # .. later
  7. session.add(A(data=numpy.int64(10)))
  8. session.commit()

In the latter case, the issue is due to the numpy.int64 datatype overriding the __eq__() method and enforcing that the return type of an expression is numpy.True or numpy.False, which breaks SQLAlchemy’s expression language behavior that expects to return ColumnElement expressions from Python equality comparisons:

  1. >>> import numpy
  2. >>> from sqlalchemy import column, Integer
  3. >>> print(column("x", Integer) == numpy.int64(10)) # works
  4. x = :x_1
  5. >>> print(numpy.int64(10) == column("x", Integer)) # breaks
  6. False

These errors are both solved in the same way, which is that special numpy datatypes need to be replaced with regular Python values. Examples include applying the Python int() function to types like numpy.int32 and numpy.int64 and the Python float() function to numpy.float32:

  1. data = numpy.int64(10)
  2. session.add(A(data=int(data)))
  3. result = session.execute(select(A.data).where(int(data) == A.data))
  4. session.commit()

SQL expression for WHERE/HAVING role expected, got True

See I’m getting errors related to “numpy.int64”, “numpy.bool_”, etc..