[Python] read geopandas dataframe with gpd.from_postgis passing parameters to sqlalchemy query

Discussão em 'Python' iniciado por Stack, Maio 6, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to pass parameters to sql query to be read with from_postgis function in order to get a geopandas dataframe from a postgis database.

    queryVMSint = text('''SELECT vmsint."Code",
    vmsint."Date",
    extract('YEAR' from "Date") as year,
    extract('MONTH' from "Date") as month,
    vmsint."Latitude",
    vmsint."Longitude",
    vmsint.geom,
    vmsint."Speed",
    vmsint."Course",
    vmsint."TrackCode",
    vmsint."Effort",
    vmsint."Id",
    vmsint."Fishing"
    FROM vms.vmsint
    JOIN vessel ON vessel."Id" = vmsint."VesselId"
    JOIN port ON port."Code" = vessel."BasePortCode"
    WHERE vmsint."Effort" = true AND port."AutonomousCommunity" = :a AND vessel."GearMainCode" = :b
    and extract('YEAR' from "Date") = :c''').bindparams(a=autonomousCommunity, b=fishingGear, c=y)

    vmsintf = gpd.GeoDataFrame.from_postgis(queryVMSint, con=engine.connect().connection, geom_col='geom')


    This code was working for me but when updating pandas to 2.2.3 the following error arises:

    Query must be a string unless using sqlalchemy.


    Does anybody know if there is a problem in .bindparams with the new versions of pandas? geopandas version = 1.0.1 sqlalchemy version = 1.4.44

    Continue reading...

Compartilhe esta Página