How to Rewrite a GeoPandas Workflow in Ibis

On this page

Problem statement

You have a GeoPandas script that works — read, filter, reproject, spatial join, group, rank — and you want it to run in DuckDB instead, for data that no longer fits in memory or for a pipeline that should not hold every row in Python. Rewriting it as SQL is a different language; rewriting it in Ibis is mostly a change of method names, because Ibis keeps the dataframe shape. This page takes one real GeoPandas workflow, cafés per square kilometre in each of Auckland's 167 suburbs from Overture Maps, rewrites it line by line in Ibis, and checks that both versions return the same table, row for row.

Quick answer

Most GeoPandas calls have a one-to-one Ibis method; the differences are the reader, convert instead of to_crs, join with a predicate instead of sjoin, and aggregate instead of a named aggregation:

cafes = places.filter(_.basic_category == "cafe").mutate(geometry=_.geometry.convert("EPSG:4326", "EPSG:2193"))
subs = suburbs.mutate(geometry=_.geometry.convert("EPSG:4326", "EPSG:2193"))
density = (subs.left_join(cafes, cafes.geometry.within(subs.geometry))
           .group_by(["id", "name"]).aggregate(cafes=_.id_right.count(), km2=(_.geometry.area() / 1e6).max())
           .mutate(per_km2=_.cafes / _.km2).order_by([ibis.desc("per_km2"), "name"]))
print(density.select("name", "cafes", "km2", "per_km2").limit(3).execute().round(2).to_string(index=False))
            name  cafes  km2  per_km2
       Newmarket     25 0.60    41.86
Auckland Central    128 3.95    32.43
    Eden Terrace     14 0.67    20.84

Newmarket has the most cafés per square kilometre, 25 on 0.60 km², or 41.86 per km², ahead of Auckland Central's 128 on 3.95 km². The Ibis version is the GeoPandas logic with Ibis's method names. _ stands for "the table at this step", as a lambda would in pandas.

Cafés per square kilometre in the three densest Auckland suburbs, computed by GeoPandas and Ibis. cafés per km², both versions agreeNewmarket41.86 (25 on 0.60 km²)Auckland Central32.43 (128 on 3.95 km²)Eden Terrace20.84 (14 on 0.67 km²)0153045945 of 980 cafés fall inside a suburb; 48 suburbs have none spatialworkflow.io
The densest suburbs for cafés, the same in GeoPandas and Ibis.

The same workflow, twice

1. GeoPandas

t0 = time.perf_counter()
g_places = gpd.read_parquet(PLACES)
g_subs = gpd.read_parquet(SUBURBS).to_crs(2193)
g_cafes = g_places[g_places.basic_category == "cafe"].to_crs(2193)
joined = gpd.sjoin(g_subs, g_cafes[["id", "geometry"]], how="left", predicate="contains")
g = (joined.groupby(["id_left", "name"]).agg(cafes=("id_right", "count"))
     .join(g_subs.set_index("id").area.rename("m2") / 1e6, on="id_left")
     .rename(columns={"m2": "km2"}).reset_index())
g["per_km2"] = g.cafes / g.km2
g = g.sort_values(["per_km2", "name"], ascending=[False, True]).reset_index(drop=True)
print(f"GeoPandas: {len(g)} suburbs, {g.cafes.sum()} of {len(g_cafes)} cafés placed, {time.perf_counter() - t0:.2f} s")
GeoPandas: 167 suburbs, 945 of 980 cafés placed, 0.16 s

The GeoPandas version reads both files into memory, reprojects, joins suburbs to the cafés they contain with a left join so suburbs without one stay, counts, and divides by area. 945 of the 980 cafés fall inside a suburb.

2. Ibis

t0 = time.perf_counter()
i = density.execute()
print(f"Ibis:      {len(i)} suburbs, {i.cafes.sum()} cafés placed, {time.perf_counter() - t0:.2f} s")
Ibis:      167 suburbs, 945 cafés placed, 0.11 s

The Ibis expression from the quick answer, executed, gives the same 167 suburbs and 945 cafés. At this size both finish in a fraction of a second. The difference is where the rows live: GeoPandas held all 56,465 places in Python, while Ibis sent DuckDB one query and got back 167 rows.

Where the rows live in the GeoPandas and Ibis versions of the same workflow. same answer, different place for the dataGeoPandas✓56,465 places in Python✓a projected copy✓each line runs at onceIbis on DuckDB✓one query to DuckDB✓167 rows back✓runs at execute()All 167 suburbs matched, in the same order spatialworkflow.io
The answer is the same; where the rows live is not.

3. Check they agree

cmp = g[["id_left", "cafes", "km2"]].merge(i[["id", "cafes", "km2"]], left_on="id_left", right_on="id", suffixes=("_gpd", "_ibis"))
print(f"suburbs in both: {len(cmp)}; same café count: {(cmp.cafes_gpd == cmp.cafes_ibis).sum()}; "
      f"largest area difference: {(cmp.km2_gpd - cmp.km2_ibis).abs().max():.1e} km²")
print("same order:", g.id_left.tolist() == i.id.tolist())
suburbs in both: 167; same café count: 167; largest area difference: 6.9e-14 km²
same order: True

Matched on the suburb id, all 167 suburbs have the same café count in both versions, and the areas differ by at most 6.9e-14 km², rounding in the last digit. The rows come out in the same order too, because both sort by density and then by name, so ties cannot fall differently.

4. The translation, call by call

GeoPandas Ibis on DuckDB
gpd.read_parquet(path) con.sql("select * replace (geometry::GEOMETRY as geometry) from read_parquet('path')")
gdf[gdf.col == v] t.filter(_.col == v)
gdf.to_crs(2193) t.mutate(geometry=_.geometry.convert("EPSG:4326", "EPSG:2193"))
gpd.sjoin(a, b, how="left", predicate="contains") a.left_join(b, b.geometry.within(a.geometry))
df.groupby(k).agg(n=("c", "count")) t.group_by(k).aggregate(n=_.c.count())
gdf.area _.geometry.area()
df["x"] = df.a / df.b t.mutate(x=_.a / _.b)
df.sort_values(["a", "b"]) t.order_by(["a", "b"])
gdf.dissolve() t.aggregate(geometry=_.geometry.unary_union())
runs line by line t.execute() runs the whole expression

Two rows carry most of the work. The reader needs the cast because Ibis 12 cannot read DuckDB's CRS-typed geometry, and convert names the source CRS because an Ibis geometry column has none.

Where the two libraries behave differently

The data used on this page

Cafés and suburbs from Overture Maps 2026-11-18.0 for Auckland, read twice, by GeoPandas and by Ibis, from a saved copy. Places CDLA Permissive 2.0, suburbs ODbL 1.0 (© OpenStreetMap contributors).

import time
from pathlib import Path

import geopandas as gpd
import ibis
import requests
from ibis import _

Path("data").mkdir(exist_ok=True)
# saved from s3://overturemaps-us-west-2/release/2026-11-18.0/ (theme=places, theme=divisions)
for layer in ["places", "suburbs"]:
    name = f"overture-auckland-{layer}-2026-11-18.0.parquet"
    if not Path(f"data/{name}").exists():
        Path(f"data/{name}").write_bytes(requests.get(f"https://data.spatialworkflow.io/{name}", timeout=300).content)
PLACES, SUBURBS = (f"data/overture-auckland-{layer}-2026-11-18.0.parquet" for layer in ["places", "suburbs"])

con = ibis.duckdb.connect()
# the cast is for Ibis 12, which cannot read DuckDB's GEOMETRY('OGC:CRS84') type (see /ibis-keyerror-ogc-crs84/)
places = con.sql(f"select * replace (geometry::GEOMETRY as geometry) from read_parquet('{PLACES}')")
suburbs = con.sql(f"select * replace (geometry::GEOMETRY as geometry) from read_parquet('{SUBURBS}')")
print(f"{places.count().execute():,} places, {suburbs.count().execute()} suburbs")
56,465 places, 167 suburbs

Example 1: no index, and suffixes on the right

print("GeoPandas join columns:", [c for c in joined.columns if c.startswith(("id", "index"))])
j = subs.left_join(cafes, cafes.geometry.within(subs.geometry))
print("Ibis join columns:     ", [c for c in j.columns if c.startswith("id") or c.endswith("_right")][:4])
print("suburbs with no café: GeoPandas", int((g.cafes == 0).sum()), "· Ibis", int((i.cafes == 0).sum()))
GeoPandas join columns: ['id_left', 'index_right', 'id_right']
Ibis join columns:      ['id', 'id_right', 'name_right', 'geometry_right']
suburbs with no café: GeoPandas 48 · Ibis 48

GeoPandas joins on the index and names the clashing columns id_left and id_right, adding index_right. Ibis tables have no index, so a join keeps the left table's names and adds _right to clashes from the right: id, id_right, name_right, geometry_right. Code that reads id_left has to change. Both keep the 48 suburbs with no café, because both were left joins counting the right-hand id, which is null for them.

Column names after a spatial join in GeoPandas and in Ibis. after the joinGeoPandasIbisleft idid_leftidright idid_rightid_rightindexindex_rightnoneright geometrydroppedgeometry_rightIbis has no index; clashes from the right get _right spatialworkflow.io
Ibis has no index, so join columns are named differently.

Example 2: nothing runs until execute

t0 = time.perf_counter()
big = places.mutate(geometry=_.geometry.convert("EPSG:4326", "EPSG:2193").buffer(250)).filter(_.geometry.area() > 0)
print(f"buffering 56,465 places, as an expression: {(time.perf_counter() - t0) * 1000:.1f} ms")
t0 = time.perf_counter()
n = big.count().execute()
print(f"running it: {n:,} rows in {time.perf_counter() - t0:.2f} s")
buffering 56,465 places, as an expression: 0.9 ms
running it: 56,465 rows in 0.21 s

Each Ibis line returns a new expression immediately; the buffer of 56,465 places is not computed until count().execute() asks for a result. A GeoPandas script fails at the line that is wrong, while an Ibis script fails at the execute that runs it. When a rewritten script fails, run execute on each intermediate expression in turn to find the step.

Why the rewrite is mostly renaming

When an Ibis expression that buffers 56,465 places is built and when it runs. building against runningbuffer(250)built in 0.9 msfilterstill nothing readexecute()56,465 rows, 0.21 sErrors surface at execute, not on the line that caused them spatialworkflow.io
Nothing runs until execute, so errors appear there.

GeoPandas and Ibis describe the same operations, a relational algebra of filters, joins, groups and computed columns, and differ in when they run them. GeoPandas evaluates each call on arrays in memory. Ibis records each call in an expression and compiles the whole chain to one SQL query when a result is asked for. That is why the translation is mostly a matter of names, and why the rewrite changes where the data lives without changing the answer, as step 3 checked. The parts that do not translate are the ones tied to memory: the index, assignment in place, and operations that need Python objects, such as explore() or a shapely function applied row by row.

Calls with no direct translation

GeoPandas calls with no direct Ibis method and what replaces each. no one-to-one methodGeoPandasIn Ibissjoin_nearestd_within join + row_number.apply(shapely function)a DuckDB function, declared.plot() / .explore()execute, set_crs, then plotsjoin(predicate='contains')within, from the other sideRewrite the steps on large tables; keep the rest in GeoPandas spatialworkflow.io
Rewrite the steps that touch large tables; keep the rest in GeoPandas.
  • sjoin_nearest. Ibis has no nearest join; a distance join ranked with row_number replaces it (nearest feature in Ibis).
  • .apply with a shapely function. Row-wise Python does not compile to SQL. Look for the DuckDB function that does the same and declare it as a built-in (functions Ibis does not wrap).
  • .plot() and .explore(). These need a GeoDataFrame: execute() the result, set its CRS, and plot that (CRS on the way out).
  • predicate="contains" turned round. GeoPandas's sjoin(subs, cafes, predicate="contains") and Ibis's cafes.geometry.within(subs.geometry) are the same test from the other side. Writing it from the side whose rows you keep is the easiest way not to invert it.

FAQ

Do I have to rewrite the whole script at once?

No. Rewrite the steps that touch the large tables. Execute the expression where the data has shrunk, and carry on in GeoPandas from there; this page's result was 167 rows, which GeoPandas handles instantly.

Is Ibis faster than GeoPandas?

Not at this size: both took a fraction of a second. What Ibis changes is memory and scale, because DuckDB streams the files instead of loading them, which the explainer measures on 623,445 buildings.