Created
December 11, 2024 03:39
-
-
Save cboettig/ae0768da02b6f5b849d3c5681b5615cb to your computer and use it in GitHub Desktop.
ibis pyarrow UDFs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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