Fixing an Ibis Geometry Column That Arrives as Binary
On this page
Problem statement
You hand Ibis a GeoDataFrame, or open a Parquet file someone exported from pandas, and the first spatial call fails with AttributeError: 'BinaryColumn' object has no attribute 'area'. The schema says why: the geometry column is binary. Ibis received the shapes as well-known binary (WKB) bytes and has no reason to treat bytes as geometry. This page reproduces it with the 167 Overture Maps suburbs of Auckland, by every common route: a GeoDataFrame, a plain DataFrame of shapely objects, a Parquet file without GeoParquet metadata, and WKT text. It then shows the one cast that fixes all of them.
Quick answer
Cast the column to geometry as soon as the table exists; DuckDB parses the WKB:
mem = ibis.memtable(suburbs_gdf)
mem = mem.mutate(geometry=mem.geometry.cast("geometry"))
print(mem.schema()["geometry"], f"{mem.geometry.convert('EPSG:4326', 'EPSG:2193').area().sum().execute() / 1e6:.1f} km²")
geospatial:geometry 656.8 km²
After the cast the column is geospatial:geometry, and the 167 suburbs measure 656.8 km² once projected to NZTM2000. The cast compiles to DuckDB's WKB parser, so it works on any column of WKB bytes whatever route brought them in.
Four ways to end up with bytes
1. A GeoDataFrame passed to memtable
mem = ibis.memtable(suburbs_gdf)
print("memtable(GeoDataFrame):", mem.schema()["geometry"])
try:
mem.geometry.area()
except AttributeError as error:
print(f"{type(error).__name__}: {error}")
memtable(GeoDataFrame): binary
AttributeError: 'BinaryColumn' object has no attribute 'area'
ibis.memtable hands the GeoDataFrame to pyarrow, and the geometry crosses as WKB in a binary column. Ibis types the expression by its schema, and a BinaryColumn has no spatial methods, so the error appears when the expression is built, before anything runs.
2. A plain DataFrame of shapely objects
plain = pd.DataFrame({"name": suburbs_gdf["name"], "geometry": list(suburbs_gdf.geometry)})
objs = ibis.memtable(plain)
print("memtable(DataFrame of shapely):", objs.schema()["geometry"])
try:
objs.geometry.area().sum().execute()
except pa.ArrowTypeError as error:
print(f"{type(error).__name__}: {error.args[0]}")
memtable(DataFrame of shapely): polygon:geometry
ArrowTypeError: Expected bytes, got a 'Polygon' object
A plain DataFrame holding shapely objects goes wrong the other way. Ibis inspects the values and types the column polygon:geometry, from the first shape, although one suburb is a MultiPolygon. Execution then fails, because the conversion to Arrow expects WKB bytes and finds a Polygon object. The schema looks right and the error comes only when the query runs.
3. A Parquet file without GeoParquet metadata
suburbs_gdf.to_wkb().to_parquet("data/suburbs_wkb.parquet") # pandas writes the WKB bytes and no "geo" metadata
print("metadata keys:", sorted(k.decode() for k in pq.read_metadata("data/suburbs_wkb.parquet").metadata))
t = con.read_parquet("data/suburbs_wkb.parquet")
print("read_parquet:", t.schema()["geometry"])
metadata keys: ['ARROW:schema', 'pandas']
read_parquet: binary
to_wkb().to_parquet() is a common way to export geometry from pandas. The file holds the same bytes as a GeoParquet file, but its metadata has only ARROW:schema and pandas keys and no geo entry. DuckDB converts a column to geometry only when that geo entry says it is one, so this column arrives as binary.
4. The cast, for bytes and for text
wkb = t.mutate(geometry=t.geometry.cast("geometry"))
text = ibis.memtable(pd.DataFrame({"name": suburbs_gdf["name"], "wkt": suburbs_gdf.geometry.to_wkt()}))
wkt = text.mutate(geometry=text.wkt.cast("geometry")).drop("wkt")
fixed_objs = ibis.memtable(plain.assign(geometry=suburbs_gdf.geometry.to_wkb().values)).mutate(geometry=_.geometry.cast("geometry"))
for label, tbl in [("WKB Parquet", wkb), ("WKT strings", wkt), ("shapely → WKB", fixed_objs)]:
km2 = tbl.geometry.convert("EPSG:4326", "EPSG:2193").area().sum().execute() / 1e6
print(f"{label:14} {tbl.schema()['geometry']} {tbl.count().execute()} suburbs, {km2:.1f} km²")
WKB Parquet geospatial:geometry 167 suburbs, 656.8 km²
WKT strings geospatial:geometry 167 suburbs, 656.8 km²
shapely → WKB geospatial:geometry 167 suburbs, 656.8 km²
The same cast("geometry") turns WKB bytes and WKT strings into geometry, and all three routes give the 167 suburbs and 656.8 km² of the original file. For the DataFrame of shapely objects, convert the shapes to WKB with to_wkb() first; Ibis cannot pass the objects themselves to DuckDB.
Joining a GeoDataFrame to a table on disk
The data used on this page
The 167 Auckland suburbs, loaded into GeoPandas first so they can be handed to Ibis, and the places they are joined to: Overture Maps 2026-11-18.0, from a saved copy. Suburbs ODbL 1.0 (© OpenStreetMap contributors), places CDLA Permissive 2.0.
from pathlib import Path
import geopandas as gpd
import ibis
import pandas as pd
import pyarrow as pa
import pyarrow.parquet as pq
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)
con = ibis.duckdb.connect()
suburbs_gdf = gpd.read_parquet("data/overture-auckland-suburbs-2026-11-18.0.parquet")
print(f"{len(suburbs_gdf)} suburbs in a GeoDataFrame, CRS {suburbs_gdf.crs}, types {suburbs_gdf.geom_type.value_counts().to_dict()}")
167 suburbs in a GeoDataFrame, CRS OGC:CRS84, types {'Polygon': 166, 'MultiPolygon': 1}
Example 1: places per suburb, from a GeoDataFrame and a file
# the cast is also needed on the file side, for a different reason: /ibis-keyerror-ogc-crs84/
places = con.sql("select * replace (geometry::GEOMETRY as geometry) from read_parquet('data/overture-auckland-places-2026-11-18.0.parquet')")
subs = ibis.memtable(suburbs_gdf[["name", "geometry"]]).mutate(geometry=_.geometry.cast("geometry"))
per = (places.join(subs, places.geometry.within(subs.geometry))
.group_by(suburb=_.name_right).aggregate(places=_.count())
.order_by(ibis.desc("places")).limit(3).execute())
print(per.to_string(index=False))
outside = places.count().execute() - places.join(subs, places.geometry.within(subs.geometry)).count().execute()
print(f"places in no suburb: {outside:,} of {places.count().execute():,}")
suburb places
Auckland Central 5971
Henderson 1828
Rosedale 1706
places in no suburb: 1,767 of 56,465
The cast suburbs join to places read straight from the file, and the join runs in DuckDB. Auckland Central has the most places, 5,971, ahead of Henderson (1,828) and Rosedale (1,706). 1,767 of the 56,465 places fall in no suburb. The suburbs cover 656.8 km² of a box roughly 2,000 km² in size, and the rest of the box, harbour, gulf islands and farmland, has places but no macrohood.
Why Ibis sees bytes
Ibis has no geometry objects of its own. A geometry column is a typed expression whose methods compile to SQL functions, and the type comes from the schema of whatever the table was built from. A GeoDataFrame, a Parquet file or an Arrow table can only tell Ibis "these are bytes" unless something marks the bytes as geometry: GeoParquet's geo metadata, or a DuckDB GEOMETRY column. GeoPandas's handover to pyarrow sends plain WKB. So the cast is not working around a bug. It tells Ibis how to read data that came in without a type, and it needs to be made once, at the point where the data enters.
Related traps
- The CRS stays behind. The GeoDataFrame's
OGC:CRS84does not travel with the bytes. Ibis computes with whatever coordinates it gets, so project withconvert, naming the source CRS yourself. - Mixed geometry types. A column inferred as
polygon:geometrycan still hold a MultiPolygon. Casting the WKB to plaingeometryavoids promising a subtype the data does not keep. - Casting late. If a
binarycolumn goes through a join first, the error appears later in the code, at the first spatial method. Cast it where the table is created.
Internal links
- Fixing KeyError: 'OGC:CRS84' when Ibis reads GeoParquet — the opposite problem: a geometry type Ibis cannot name
- How to read GeoParquet files into Ibis — files that arrive as geometry
- Fixing Ibis output with no CRS — the CRS the GeoDataFrame had and the memtable dropped
- How to rewrite a GeoPandas workflow in Ibis — where a GeoDataFrame usually meets an Ibis table
FAQ
Is the cast slow?
No more than reading the file. It compiles to DuckDB's WKB parser, the same step DuckDB takes for a GeoParquet file. Example 1 joined the 167 cast suburbs to 56,465 places in about two seconds.
Should I use memtable or write the GeoDataFrame to Parquet first?
For a few thousand shapes, memtable with the cast is simplest. For larger frames, to_parquet writes GeoParquet with its geo metadata, which DuckDB reads as geometry. Ibis 12 then needs the other cast, from GEOMETRY('OGC:CRS84') to GEOMETRY (that fix), and the data does not have to sit in memory on both sides.