openego / eGon-data

GNU Affero General Public License v3.0
10 stars 4 forks source link

Import sqlalchemy adapter #975

Closed nailend closed 1 year ago

nailend commented 1 year ago

@nesnoj and me are using the following workaround for data request with sqlalchemy and read_sql e.g here Without the workaround the array of int profile_ids raises an error.

My questions is, where is the best place to put this to not have it multiple times in the code? Is it possible to import the script as a module? From my debugging experience, I had to have this in every top level function of every dataset e.g here @nesnoj only imports it once per script e.g. here

Is it possible to import it once for all tasks or might this trigger new conflicts?

from psycopg2.extensions import AsIs, register_adapter

def adapt_numpy_float64(numpy_float64):
    return AsIs(numpy_float64)

def adapt_numpy_int64(numpy_int64):
    return AsIs(numpy_int64)

# ========== Register np datatypes with SQLA ==========
register_adapter(np.float64, adapt_numpy_float64)
register_adapter(np.int64, adapt_numpy_int64)
# =====================================================
gnn commented 1 year ago

Could you elaborate? Do you mean once per Python file? In what context? The eGon-data codebase, standalone scripts, bigger external applications or tests for any of those? Also, is this related to the fact that pandas/GeoPandas doesn't correctly cast column datatypes when using read_sql, or does this just help with writing stuff to the database?

nailend commented 1 year ago

Sorry @gnn this was first meant as a reminder for @nesnoj to approach you at the project meeting. I updated the description. Thanks, looking forward to your answer

nesnoj commented 1 year ago

I think we won't implement a general solution due to lack of time, I recommend to close this.