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.
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.
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.
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
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
sjoin_nearest. Ibis has no nearest join; a distance join ranked withrow_numberreplaces it (nearest feature in Ibis)..applywith 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'ssjoin(subs, cafes, predicate="contains")and Ibis'scafes.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.
Internal links
- Ibis for spatial data explained — what changes when the dataframe is an expression
- How to find the nearest feature in Ibis — the one GeoPandas join Ibis has no method for
- Fixing an Ibis geometry column that arrives as binary — passing an existing GeoDataFrame into Ibis
- Fixing Ibis output with no CRS — taking the result back to GeoPandas
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.