save

save(
    df,
    name,
    scale,
    directory='.',
    title=None,
    description='',
    variables=None,
    sources=(),
    citation=None,
    license='CC-BY-4.0',
)

Save a panel exactly like the published datasets, ready to share or contribute.

Parameters

Name Type Description Default
df pd.DataFrame A valid panel (see geo.check) required
name str The dataset id: lowercase letters, digits, - or _ required
scale str Its scale required
directory Where to write data/ and metadata/ (default: the current folder) '.'
title str | None What the dataset is None
description str | None What the dataset is None
variables dict | None {column: {"label", "description", "unit", "aggregation", "source"}} None
sources Titles or {"title", "path"} dicts of the sources ()
citation str | None The recommended citation None
license str An SPDX license id (default CC-BY-4.0) 'CC-BY-4.0'

Notes

Writes <directory>/data/<scale>/<name>.csv and .parquet and <directory>/metadata/<name>.datapackage.json (Frictionless). Describe each variable in variables: {"pop": {"label": "Population", "description": "Sum of WorldPop 100 m population over the unit", "unit": "people", "aggregation": "sum", "source": "WorldPop"}}. The label is a readable name for plots; the description says how the variable was built. The aggregation rule (sum, mean, weighted_mean:<field>, none, …) tells geo.aggregate() how to move the variable to coarser scales.

Returns the datapackage as a dict.

Example

!pip install -q "geo4bolivia[raster]"
import numpy as np
import rasterio
from rasterio.transform import from_bounds
import geo4bolivia as geo
import plotly.express as px

# A small raster of rough distance to La Paz (km); use your own GeoTIFF instead
x0, y0, x1, y1 = geo.boundaries("country").total_bounds
lon, lat = np.meshgrid(np.linspace(x0, x1, 400), np.linspace(y1, y0, 400))
km = 111 * np.hypot(lon + 68.13, lat + 16.50)
with rasterio.open("dist.tif", "w", driver="GTiff", width=400, height=400,
                   count=1, dtype="float32", crs="EPSG:4326",
                   transform=from_bounds(x0, y0, x1, y1, 400, 400)) as f:
    f.write(km.astype("float32"), 1)

dist = geo.zonal("dist.tif", scale="province", stats=["mean"],
                 name="dist_lpz", year=2024)
pkg = geo.save(dist, "distance", scale="province", directory="my_data",
               title="Distance to La Paz",
               variables={"dist_lpz_mean": {"label": "Distance to La Paz (km)",
                          "unit": "km", "aggregation": "none"}})
print(pkg["resources"][0]["schema"]["fields"][2])
wrote my_data/data/province/distance.csv, my_data/data/province/distance.parquet and my_data/metadata/distance.datapackage.json
{'name': 'dist_lpz_mean', 'title': 'Distance to La Paz (km)', 'type': 'number', 'description': '', 'geo:unit': 'km', 'geo:aggregation': 'none', 'geo:source': ''}