Skip to content

Commit c656dda

Browse files
authored
docs: add quickstart and note gcsfs requirement (#188)
- Add a short, dependency-light Quickstart (air_temperature + MAX) near the top of the README so the first example is fast to read and run, and demote the heavy ARCO-ERA5 walkthrough to 'A bigger example'. - Note that the gs:// ARCO-ERA5 example requires gcsfs, in both the README and docs/examples.md. Closes #184 Closes #185 ```python import xarray as xr import xarray_sql as xql ds = xr.tutorial.open_dataset('air_temperature') ctx = xql.XarrayContext() ctx.from_dataset('air', ds, chunks=dict(time=100)) 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 ''') # Option 1: a flat pandas DataFrame. clim.to_pandas().head() # Option 2: round-trip back to an Xarray Dataset and plot the annual cycle as # a time series. `month` is a derived column, so name it as the dimension; the # variable's units are recovered from the registered table. clim_ds = clim.to_dataset(dims=["month"]) clim_ds["air"].plot() ``` <img width="600" height="429" alt="image" src="https://github.com/user-attachments/assets/eaeca35b-ad8b-4ce5-94b3-a421a0dbd29a" />
1 parent 6fede7a commit c656dda

2 files changed

Lines changed: 77 additions & 9 deletions

File tree

README.md

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,57 @@ pip install xarray-sql
1414
## What is this?
1515

1616
This is an experiment to provide a SQL interface for array datasets.
17+
Succinctly, we "pivot" Xarray Datasets to treat them like tables so we can run
18+
SQL queries against them.
19+
20+
## Quickstart
21+
22+
Open a Dataset, register it as a table with `from_dataset`, compute a
23+
climatology in SQL, then write the result back to Xarray and plot it:
24+
25+
> **Note:** this example also needs `pooch` and a netCDF backend (for the
26+
> tutorial download) and `matplotlib` (for the plot):
27+
> `pip install pooch netCDF4 matplotlib`.
28+
29+
```python
30+
import xarray as xr
31+
import xarray_sql as xql
32+
33+
# 4x-daily surface air temperature on a lat/lon grid, 2013-2014.
34+
ds = xr.tutorial.open_dataset('air_temperature')
35+
36+
ctx = xql.XarrayContext()
37+
ctx.from_dataset('air', ds, chunks=dict(time=100))
38+
39+
# A climatology — the mean annual cycle — computed in SQL: average air
40+
# temperature for each month of the year, over all grid cells and years.
41+
clim = ctx.sql('''
42+
SELECT
43+
CAST(date_part('month', "time") AS INTEGER) AS month,
44+
AVG("air") AS air
45+
FROM "air"
46+
GROUP BY CAST(date_part('month', "time") AS INTEGER)
47+
ORDER BY month
48+
''')
49+
50+
# Write the SQL result back to an Xarray Dataset. `month` is a derived
51+
# column, so name it as the dimension; the variable's units are recovered
52+
# from the registered table. The result is one value per month: air(month).
53+
clim_ds = clim.to_dataset(dims=["month"])
54+
55+
# Plot the annual cycle as a time series.
56+
clim_ds["air"].plot() # in a script, call matplotlib.pyplot.show() to display
57+
```
58+
59+
That's the round trip — Xarray in, SQL in the middle, Xarray (and a plot) back
60+
out.
61+
62+
## A bigger example: ARCO-ERA5
63+
64+
The same interface scales to cloud-native datasets with hundreds of variables,
65+
like [ARCO-ERA5](https://github.com/google-research/arco-era5).
66+
67+
> **Note:** reading from `gs://` requires `gcsfs` (`pip install gcsfs`).
1768
1869
```python
1970
import xarray as xr
@@ -105,9 +156,6 @@ ctx.sql('''
105156
_(A runnable version of this example lives at
106157
[`perf_tests/era5_temp_profile.py`](perf_tests/era5_temp_profile.py).)_
107158

108-
Succinctly, we "pivot" Xarray Datasets to treat them like tables so we can run
109-
SQL queries against them.
110-
111159
## Why build this?
112160

113161
A few reasons:

docs/examples.md

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,43 @@
11
# Examples
22

3+
A query result can be consumed two ways: as a flat pandas DataFrame
4+
(`to_pandas`) or written back to an Xarray Dataset (`to_dataset`). This computes
5+
a climatology — the mean annual cycle, one value per month of the year — and
6+
shows both.
7+
8+
> **Note:** this example also needs `pooch` and a netCDF backend (for the
9+
> tutorial download) and `matplotlib` (for the plot):
10+
> `pip install pooch netCDF4 matplotlib`.
11+
312
```python
413
import xarray as xr
514
import xarray_sql as xql
615

716
ds = xr.tutorial.open_dataset('air_temperature')
817

918
ctx = xql.XarrayContext()
10-
ctx.from_dataset('air', ds, chunks=dict(time=24))
19+
ctx.from_dataset('air', ds, chunks=dict(time=100))
1120

12-
result = ctx.sql('''
21+
clim = ctx.sql('''
1322
SELECT
14-
"lat", "lon", AVG("air") as air_avg
23+
CAST(date_part('month', "time") AS INTEGER) AS month,
24+
AVG("air") AS air
1525
FROM
1626
"air"
1727
GROUP BY
18-
"lat", "lon"
28+
CAST(date_part('month', "time") AS INTEGER)
29+
ORDER BY
30+
month
1931
''')
2032

21-
df = result.to_pandas()
22-
df.head()
33+
# Option 1: a flat pandas DataFrame.
34+
clim.to_pandas().head()
35+
36+
# Option 2: round-trip back to an Xarray Dataset and plot the annual cycle as
37+
# a time series. `month` is a derived column, so name it as the dimension; the
38+
# variable's units are recovered from the registered table.
39+
clim_ds = clim.to_dataset(dims=["month"])
40+
clim_ds["air"].plot()
2341
```
2442

2543
## Mixed-dimension datasets: ARCO-ERA5
@@ -35,6 +53,8 @@ Open a year of ARCO-ERA5 and let SQL `WHERE` clauses do the filtering — the
3553
library prunes time partitions and pushes dimension-column filters down. Use
3654
the `table_names` kwarg to give each dimension group a friendly name:
3755

56+
> **Note:** reading from `gs://` requires `gcsfs` (`pip install gcsfs`).
57+
3858
```python
3959
import xarray as xr
4060
import xarray_sql as xql

0 commit comments

Comments
 (0)