xarray-sql
Query Xarray with SQL
pip install xarray-sql
What is this?
This is an experiment to provide a SQL interface for raster data.
import xarray as xr
import xarray_sql as qr
ds = xr.tutorial.open_dataset('air_temperature')
# The same as a dask-sql Context; i.e. an Apache DataFusion Context.
c = qr.XarrayContext()
c.from_dataset('air', ds, chunks=dict(time=24))
df = c.sql('''
SELECT
"lat", "lon", AVG("air") as air_total
FROM
"air"
GROUP BY
"lat", "lon"
''')
# A table of the average temperature for each location across time.
df.to_pandas()
# Alternatively, you can just create the DataFrame from the Dataset:
df = qr.read_xarray(ds).to_pandas()
df.head()
Succinctly, we "pivot" Xarray Datasets to treat them like tables so we can run SQL queries against them.
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 Pangeo Forge. 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 Pangeo is a scientific database. Here, xarray-sql can be thought of as a missing DB front end.
How does it work?
All chunks in an 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!
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. to_dataframe()
just changes this metadata (via a ravel()/reshape()), back into a column
amenable to a DataFrame.
There is added overhead from duplicating dimensions as columns, which we see as worth the convenience of DataFrames.
What are the current limitations?
2025 update: TBD, datafusion provides a whole new world!
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.
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.
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.
Some sources are re-distributed from Google LLC via https://github.com/google/Xee (also Apache-2.0 License) with and without modification (specifically, Github Actions workflows). These files are subject to the original copyright; they include the original license header comment as well as a note to indicate modifications (when appropriate).
Metadata
Release files for xarray_sql 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| xarray_sql-0.1.1.tar.gz | 1.5 MB | Without provenance | Details for xarray_sql-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| xarray_sql-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for xarray_sql-0.1.1-py3-none-any.whl |
Total release size: 1.6 MB
Release files / xarray_sql-0.1.1.tar.gz
| Download URL | xarray_sql-0.1.1.tar.gz |
|---|---|
| Size | 1.5 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
366e301b4448a0cbdaf7e691443cc1a835bf7ce6a27e5ec9fae32e2f85078e50
|
|
BLAKE2b-256 checksum How to use checksums |
85a942ecb8cc10c0be39a276e6f02f35f9b10bcd81e0b4b0ca4afc5b52d3357a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.23
|
Release files / xarray_sql-0.1.1-py3-none-any.whl
| Download URL | xarray_sql-0.1.1-py3-none-any.whl |
|---|---|
| Size | 16.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
170c9b03c2b73bb51b9ffdd2e415dc5452570f576ab541b1bc35d0a38227f26c
|
|
BLAKE2b-256 checksum How to use checksums |
862f7c06e60b1ea4ef25ab2cf26b467d497db5956eb9ea8895a1074f92dfb9f4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.23
|
Security for xarray_sql 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.