Skip to content

Instantly share code, notes, and snippets.

@cboettig
Created December 11, 2024 03:39
Show Gist options
  • Save cboettig/ae0768da02b6f5b849d3c5681b5615cb to your computer and use it in GitHub Desktop.
Save cboettig/ae0768da02b6f5b849d3c5681b5615cb to your computer and use it in GitHub Desktop.
ibis pyarrow UDFs
import ibis
from ibis import _
import leafmap.maplibregl as leafmap
con = ibis.duckdb.connect(extensions=["spatial"])
states = con.read_geo("https://raw.githubusercontent.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json")
## Add a fill-color column
import matplotlib.cm as cm
import matplotlib.colors as mcolors
import pyarrow
# .Float64(nullable=True)
@ibis.udf.scalar.pyarrow
def viridis(x: pyarrow.float64()) -> str:
rgb = cm.viridis(x)
return mcolors.to_hex(rgb)
# nope, custom udf function fails
column = "density"
gdf = (states
.mutate(norm = _[column] / _[column].max()) # normalize counts to between 0-1
.mutate(fill = viridis(_.norm))
.execute()
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment