Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

xarray-sql

Query Xarray with SQL

PyPI Version ci lint ci-build ci-rust PyPI Downloads PyPI Downloads

pip install xarray-sql

What is this?

This is an experiment to provide a SQL interface for array datasets. Succinctly, we "pivot" Xarray Datasets to treat them like tables so we can run SQL queries against them — on the query engine of your choice. xarray-sql translates data, not queries: it registers a lazy Dataset as a table on DataFusion (built in), DuckDB, or Polars, and turns any engine's Arrow result back into a labeled Dataset. Dialects, geometry functions, and optimizers stay with the engine.

Quickstart

Open a Dataset, register it as a table with from_dataset, compute a climatology in SQL, then write the result back to Xarray and plot it:

Note: this example also needs pooch and a netCDF backend (for the tutorial download) and matplotlib (for the plot): pip install pooch netCDF4 matplotlib.

import xarray as xr
import xarray_sql as xql

# 4x-daily surface air temperature on a lat/lon grid, 2013-2014.
ds = xr.tutorial.open_dataset('air_temperature')

ctx = xql.XarrayContext()
ctx.from_dataset('air', ds, chunks=dict(time=100))

# A climatology — the mean annual cycle — computed in SQL: average air
# temperature for each month of the year, over all grid cells and years.
clim = ctx.sql('''
  SELECT
    CAST(date_part('month', "time") AS INTEGER) AS month,
    AVG("air") AS air
  FROM "air"
  GROUP BY CAST(date_part('month', "time") AS INTEGER)
  ORDER BY month
''')

# Round-trip the result back to Xarray. `month` is a derived column, so name
# it as the dimension.
clim_ds = clim.to_dataset(dims=["month"])

# Plot the annual cycle as a time series.
clim_ds["air"].plot()  # in a script, call matplotlib.pyplot.show() to display

That's the round trip — Xarray in, SQL in the middle, Xarray (and a plot) back out.

The same Dataset registers on other engines with one call — DuckDB gets a native lazy table with predicate pushdown, Polars scans the same object:

import duckdb

con = duckdb.connect()
xql.register(con, 'air', ds, chunks=dict(time=100))
rel = con.sql('SELECT time, AVG("air") AS air FROM air GROUP BY time ORDER BY time')
xql.to_dataset(rel, template=ds)   # any engine's Arrow result round-trips

See Engines for the support matrix, DuckDB/Polars details, and the lazy chunked round-trip.

A bigger example: ARCO-ERA5

The same interface scales to cloud-native datasets with hundreds of variables, like ARCO-ERA5.

Note: reading from gs:// requires gcsfs (pip install gcsfs).

import xarray as xr
import xarray_sql as xql


# Open ARCO-ERA5 — a weather dataset with 273 variables since 1940. 
# Turning off dask means we don't have to wait to construct a task graph.
ds = xr.open_zarr(
  'gs://gcp-public-data-arco-era5/ar/full_37-1h-0p25deg-chunk-1.zarr-v3',
  chunks=None,  # Turn dask off
  storage_options={'token': 'anon'}  # Anonymous read from the public GCS bucket — no auth required.
)

ctx = xql.XarrayContext()
# Make sure to pass `chunks`!
ctx.from_dataset('era5', ds, chunks=dict(time=6), table_names={
    ('time', 'latitude', 'longitude'): 'surface',
    ('time', 'level', 'latitude', 'longitude'): 'atmosphere',
})
# Registration takes ~10s on my machine.

# Heads up: ARCO-ERA5 has 262 surface + 11 atmospheric variables. The library
# pushes column projection down to Zarr, so SELECT only fetches what you ask
# for — but `SELECT * FROM era5.surface` would try to pull every variable
# across the year (terabytes from GCS). 
#  ---> Always SELECT specific columns. <---

# Average 2m-temperature over NYC on the morning of 2020-01-01. The library
# pushes WHERE clauses on dimension columns down to partition pruning.
ctx.sql('''
  SELECT AVG("2m_temperature") - 273.15 AS avg_c
  FROM era5.surface
  WHERE time BETWEEN TIMESTAMP '2020-01-01'
                 AND TIMESTAMP '2020-01-01 05:00:00'
    AND latitude  BETWEEN 39 AND 40
    AND longitude BETWEEN 286 AND 287  -- ERA5 uses 0-360 longitudes
''').to_pandas()
#       avg_c
# 0  8.640069

# Average temperature per pressure level, globally. 
result = ctx.sql('''
  SELECT level, AVG(temperature) - 273.15 AS avg_c
  FROM era5.atmosphere
  WHERE time BETWEEN TIMESTAMP '2020-01-01'
                 AND TIMESTAMP '2020-01-01 05:00:00'
  GROUP BY level
  ORDER BY level DESC
''')
# DataFrame()
# +-------+----------------------+
# | level | avg_c                |
# +-------+----------------------+
# | 1000  | 6.6210120796502565   |
# | 975   | 5.185637919348153    |
# | 950   | 4.028428657263021    |
# | 925   | 3.0828117974912743   |
# | 900   | 2.2109172992531967   |
# | 875   | 1.395017610194202    |
# | 850   | 0.6342670572626616   |
# | 825   | -0.21037158786759846 |
# | 800   | -1.1810754318269687  |
# | 775   | -2.3064649711534457  |
# +-------+----------------------+

# `latitude`/`longitude` are inferred from the registered table's surviving
# dims; `template` is kept only to recover metadata (attrs, encoding).
ctx.sql('''
  SELECT latitude, longitude, AVG("2m_temperature") - 273.15 AS avg_c
  FROM era5.surface
  WHERE time BETWEEN TIMESTAMP '2020-01-01'
                 AND TIMESTAMP '2020-01-01 05:00:00'
  GROUP BY latitude, longitude
  ORDER BY latitude DESC, longitude
''').to_dataset(template=ds)
# <xarray.Dataset> Size: 8MB
# Dimensions:    (latitude: 721, longitude: 1440)
# Coordinates:
#   * latitude   (latitude) float32 3kB 90.0 89.75 89.5 ... -89.5 -89.75 -90.0
#   * longitude  (longitude) float32 6kB 0.0 0.25 0.5 0.75 ... 359.2 359.5 359.8
# Data variables:
#     avg_c      (latitude, longitude) float64 8MB -26.84 -26.84 ... -27.38 -27.38
# Attributes:
#     last_updated:           2026-06-20 02:33:34.265980+00:00
#     valid_time_start:       1940-01-01
#     valid_time_stop:        2025-12-31
#     valid_time_stop_era5t:  2026-06-14

(A runnable version of this example lives at perf_tests/era5_temp_profile.py.)

Why build this?

A few reasons:

  • Even though SQL is the lingua franca of data, scientific datasets are often inaccessible to non-scientists (SQL users).
  • Joining tabular data with raster data is common yet difficult. It could be easy.
  • There are many cloud-native, Xarray-openable datasets, from Google Earth Engine to the Source Cooperative. Wouldn’t it be great if these were also SQL-accessible? How can the bridge be built with minimal effort?

This is a light-weight way to prove the value of the interface.

The larger goal is to explore the hypothesis that the Pangeo ecosystem is a scientific database. Here, xarray-sql can be thought of as a missing DB front end.

How does it work?

All chunks in a Xarray Dataset are transformed into a Dask DataFrame via from_map() and to_dataframe(). For SQL support, we just use dask-sql. That's it!

2025 update: This library now implements a Dask-like from_map interface in pure DataFusion and PyArrow, but works with the same principle!

2026 update: Instead of from_map(), we create a way to translate Xarray chunks into Arrow RecordBatches. We pass a Python callback into a DataFusion TableProvider that lets the DB engine translate the underlying Dataset arrays into DataFusion partitions. The same chunks-to-batches translation is also exposed as a pyarrow.dataset.Dataset with predicate and projection pushdown, which is how DuckDB and Polars consume registered Datasets with no engine-specific code. Ultimately, the initial insight of the pivot() function -- that any ndarray can be translated into a 2D table -- underlies this performant query mechanism.

Does it work?

Yes. The recurring worry is that the SQL interface is a toy — fine for SELECTs, but not for the operations geoscience actually runs. So we wrote a suite that takes the staples of geospatial and climate analysis — the ones we assume need an array library — and expresses each one in SQL, then checks the SQL answer against an xarray/array reference to floating-point tolerance:

  • Spectral indices (NDVI) — column arithmetic over a real Sentinel-2 scene.
  • Climatology, anomalies, zonal meansGROUP BY and self-JOIN against the 0.25° ARCO-ERA5 archive registered as a lazy table. Each query is bounded to a small window (a few days over a region) and reads only that slice — the point is that you can aim a query at a multi-decade archive and pay only for the data it asks for, not that the query scans the whole record.
  • Forecast skill — scoring the Pangu-Weather and GraphCast ML models against ERA5 (WeatherBench 2) as a JOIN on valid_time = init + lead; it reproduces the published result that GraphCast beats Pangu at every lead.
  • Raster × vector zonal stats — a range JOIN of the ERA5 grid against a table of regions.
  • Reprojection and regridding — a reproject(x, y, src_crs, dst_crs) scalar PROJ UDF, shipped as the optional geo extension (pip install xarray-sql[geo], validated against Earth Engine's own geodesy via Xee) and a sparse-weight-table JOIN (regridding real SRTM terrain).

Every case matches its array reference. The headline finding: these operations are not really "array" operations at all — they are GROUP BY, JOIN, window functions, and CASE in disguise, and a query engine runs them at scale. See benchmarks/geospatial/ and the write-up, Geospatial operations are relational operations.

Why does this work?

Underneath Xarray, Dask, and Pandas, there are NumPy arrays. These are paged in chunks and represented contiguously in memory. It is only a matter of metadata that breaks them up into ndarrays. pivot(), which uses to_dataframe(), just changes this metadata (via a ravel()/reshape()), back into a column amenable to a DataFrame. We take advantage of this lightweight metadata change to make chunked information scannable by a DB engine (DataFusion, DuckDB, Polars — anything that speaks Arrow).

What are the current limitations?

The sharp edges we know about — per engine and fundamental — are cataloged in Known issues & limitations. Currently, we're looking for early users – "tire kickers", if you will. We'd love your input to shape the direction of this project! Please, give this a try and file issues as you see fit. Check out our contributing guide, too 😉.

What would a deeper integration look like?

I have a few ideas so far. One approach involves applying operations directly on Xarray Datasets. This approach is being pursued here, as xql.

Deeper still: I was thinking we could make a virtual filesystem for parquet that would internally map to Zarr. Raster-backed virtual parquet would open up integrations to numerous tools like dask, pyarrow, duckdb, and BigQuery. More thoughts on this in #4.

2025 update: Something like this is being built across a few projects! The ones I know about are:

2026 update: A colleague and I are experimenting with native Zarr RDBMS engines. Check out:

Roadmap

  • Lazy evaluation via the pyarrow Dataset interface #93. Implemented in #100
  • Support proper parallelism via proper partition handling on the rust/datafusion side. #106
  • Support core datafusion optimizations to scan less data, like #104, ...
  • Translate a single Zarr to a collection of tables #85.
  • Distributed beyond a single node through the DataFusion integration with Ray Datasets #68 or Apache Ballista #98.
  • Demo: calculate Sea Surface Temperature from 1940 - Present in SQL #36.
  • Provide an option to integrate DataFusion directly to Zarr via Rust #4.
  • (To be formally announced eventually): The 100 Trillion Row Challenge #34.

Sponsors & Contributors

I want to give a special thanks to the following folks and institutions:

  • Pramod Gupta and the Anthromet Team at Google Research for the problem formation and design inspiration.
  • Jake Wall and AI2/Ecoscope for compute resources and key use cases.
  • Charles Stern, Stephan Hoyer, Alexander Kmoch, Wei Ji, and Qiusheng Wu for the early review and discussion of this project.
  • Tom Nichols, Kyle Barron, Tom White, and Maxime Dion for the Array Working Group and DataFusion-specific collaboration.
  • The gracious volunteer data science students at UCSD's DS3 org, who are working to make this library better.
  • Andrew Huang for the sense of taste he brings to the project and consummate code changes.
  • Aman Kumar for spending a considerable amount of his GSoC internship contributing to this project.

License

Copyright 2024 Alexander Merose

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

All vendored code has proper license attribution.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xarray_sql-0.4.0rc1.tar.gz (234.7 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

xarray_sql-0.4.0rc1-cp310-abi3-win_amd64.whl (19.3 MB view details)

Uploaded CPython 3.10+Windows x86-64

xarray_sql-0.4.0rc1-cp310-abi3-manylinux_2_34_x86_64.whl (22.6 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.34+ x86-64

xarray_sql-0.4.0rc1-cp310-abi3-manylinux_2_28_x86_64.whl (20.1 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.28+ x86-64

xarray_sql-0.4.0rc1-cp310-abi3-manylinux_2_28_aarch64.whl (18.7 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.28+ ARM64

xarray_sql-0.4.0rc1-cp310-abi3-macosx_11_0_arm64.whl (17.7 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

xarray_sql-0.4.0rc1-cp310-abi3-macosx_10_12_x86_64.whl (18.5 MB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

Details for the file xarray_sql-0.4.0rc1.tar.gz.

File metadata

  • Download URL: xarray_sql-0.4.0rc1.tar.gz
  • Upload date:
  • Size: 234.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for xarray_sql-0.4.0rc1.tar.gz
Algorithm Hash digest
SHA256 298659a5ec187e0efc95e45f6d438999d4c0fb965d90c1580fea8081d37c4f76
MD5 1947d9a44f3c9504fdf8cbc0f169ef28
BLAKE2b-256 ff68299b4430db14544d7b989f00e0096e66ad61da69fad2b45f3e60ccdee6fd

See more details on using hashes here.

File details

Details for the file xarray_sql-0.4.0rc1-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: xarray_sql-0.4.0rc1-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 19.3 MB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for xarray_sql-0.4.0rc1-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 9ae362240710d7e8bc3afc43173bb909fa87ce94523306fc5ab10513fd0c197b
MD5 64f3cd3d2c3bad3938e1614ac3ddd704
BLAKE2b-256 2e3ee05fe0871d22f7503e025f5e42e015838551d6ce9b905302e86259b3ccca

See more details on using hashes here.

File details

Details for the file xarray_sql-0.4.0rc1-cp310-abi3-manylinux_2_34_x86_64.whl.

File metadata

  • Download URL: xarray_sql-0.4.0rc1-cp310-abi3-manylinux_2_34_x86_64.whl
  • Upload date:
  • Size: 22.6 MB
  • Tags: CPython 3.10+, manylinux: glibc 2.34+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for xarray_sql-0.4.0rc1-cp310-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 5430c2cd9d8752ba97d783d37db9179af58a3eb8f5325f6f5ac2f4763dbd539d
MD5 9258599de64a7acef3e3cbfb7ed6be94
BLAKE2b-256 f614102e0fd4c0e92e30ba14be536108fe094f61d6d3500f8978e41b44804e72

See more details on using hashes here.

File details

Details for the file xarray_sql-0.4.0rc1-cp310-abi3-manylinux_2_28_x86_64.whl.

File metadata

  • Download URL: xarray_sql-0.4.0rc1-cp310-abi3-manylinux_2_28_x86_64.whl
  • Upload date:
  • Size: 20.1 MB
  • Tags: CPython 3.10+, manylinux: glibc 2.28+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for xarray_sql-0.4.0rc1-cp310-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 04e7dd31b918812c3ace6d00d9e27b00a9356b639df536b0b40451acc91d3706
MD5 c1262d82d331cae786d3e71026d2f3cc
BLAKE2b-256 2f033525528db15a3386f8496fa293ba8cae42801fe476d91b693f0870b19fee

See more details on using hashes here.

File details

Details for the file xarray_sql-0.4.0rc1-cp310-abi3-manylinux_2_28_aarch64.whl.

File metadata

  • Download URL: xarray_sql-0.4.0rc1-cp310-abi3-manylinux_2_28_aarch64.whl
  • Upload date:
  • Size: 18.7 MB
  • Tags: CPython 3.10+, manylinux: glibc 2.28+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for xarray_sql-0.4.0rc1-cp310-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 8e46db4f3125060c36035caac12cb9ef51ca57e91725aa761f8da4e81a297eac
MD5 1c3df4eeb24a0bfe68721f55d162800e
BLAKE2b-256 0aae8440c58b5e1b88f314fad2864974fc96577b92d471c1c5e0cc8a2105d301

See more details on using hashes here.

File details

Details for the file xarray_sql-0.4.0rc1-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

  • Download URL: xarray_sql-0.4.0rc1-cp310-abi3-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 17.7 MB
  • Tags: CPython 3.10+, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for xarray_sql-0.4.0rc1-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f5fedd28219620bb95c7f628032a5bb093c9a328f3c622db3e7da9d585adf17b
MD5 5b956220a218fcf7f7b117306c9e725a
BLAKE2b-256 ad79e42e9a243a4439bcab77582df446b159b2cf04d2f6d2cfb164a8b6ece791

See more details on using hashes here.

File details

Details for the file xarray_sql-0.4.0rc1-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

  • Download URL: xarray_sql-0.4.0rc1-cp310-abi3-macosx_10_12_x86_64.whl
  • Upload date:
  • Size: 18.5 MB
  • Tags: CPython 3.10+, macOS 10.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for xarray_sql-0.4.0rc1-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c1c083f663c5567edae0a7fe469f6bdadb141856f6bec56c11eb8affc3e8d3e0
MD5 c1659a6e821b665789a8a14e715cf424
BLAKE2b-256 58e6f683be3679f6a1d1f3039f50f5022cdb41bb1a6785ec956eb0f9d1a2f5c4

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.4.0rc1 This release

7 files

0.3.3

7 files

0.3.2

7 files

0.3.1

7 files

0.3.0

19 files

0.2.3

13 files

0.2.2

13 files

0.2.1

13 files

0.2.0

13 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

0.0.2

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page