You can run this notebook in a live session Binder or view it on Github.



Welcome to the Xarray Tutorial.

Xarray is an open source project and Python package that makes working with labelled multi-dimensional arrays simple, efficient, and fun!

Xarray introduces labels in the form of dimensions, coordinates and attributes on top of raw NumPy-like arrays, which allows for a more intuitive, more concise, and less error-prone developer experience. The package includes a large and growing library of domain-agnostic functions for advanced analytics and visualization with these data structures.

Xarray is inspired by and borrows heavily from pandas, the popular data analysis package focused on labelled tabular data. It is particularly tailored to working with netCDF files, which were the source of Xarray’s data model, and integrates tightly with Dask for parallel computing.

Tutorial Setup

This tutorial is designed to run on Binder. This will allow you to run the turoial in the cloud without any additional setup. To get started, simply click here: Binder

If you choose to install the tutorial locally, follow these steps:

  1. Clone the repository:

    git clone
  2. Install the environment. The repository includes an environment.yaml in the .binder subdirectory that contains a list of all the packages needed to run this tutorial. To install them using conda run:

    conda env create -f .binder/environment.yml
    conda activate xarray
  3. Start a Jupyter session:

    jupyter lab

Tutorial Structure

This tutorial is made up of multiple Jupyter Notebooks. These notebooks mix code, text, visualization, and exercises.

If you haven’t used JupyterLab before, it’s similar to the Jupyter Notebook. If you haven’t used the Notebook, the quick intro is

  1. There are two modes: command and edit

  2. From command mode, press Enter to edit a cell (like this markdown cell)

  3. From edit mode, press Esc to change to command mode

  4. Press shift+enter to execute a cell and move to the next cell.

  5. The toolbar has commands for executing, converting, and creating cells.

The layout of the tutorial will be as follows:

  1. Introduction + Data structures for multi-dimensional data

  2. Working with labeled data

  3. Computation with Xarray

  4. Plotting and Visualization

  5. Introduction to Dask

  6. Dask and Xarray

Exercise: Print Hello, world!

Each notebook will have exercises for you to solve. You’ll be given a blank or partially completed cell, followed by a hidden cell with a solution. For example.

Print the text “Hello, world!”.

# Your code here

In some cases, the next cell will have the solution. Click the ellipses to expand the solution, and always make sure to run the solution cell, in case later sections of the notebook depend on the output from the solution.

print("Hello, world!")
Hello, world!

Going Deeper

We’ve designed the notebooks above to cover the basics of Xarray from beginning to end. To help you go deeper, we’ve also create a list of notebooks that demonstrate real-world applications of Xarray in a variety of use cases. These need not be explored in any particular sequence, instead they are meant to provide a sampling of what Xarray can be used for.

Xarray and Weather/Climate Model Data

  1. Global Mean Surface Temperature from CMIP6: Start with global_mean_surface_temp.ipynb then feel free to explore the rest of the notebooks.

  2. National Water Model Streamflow Analysis: Start with 02_National_Water_Model.ipynb then feel free to explore the rest of the notebooks.

Xarray and Satellite Data

  1. Landsat-8 on AWS

Xarray and Baysian Statistical Modeling

  1. Xarray and PyMC3: Start with multilevel_modeling.ipynb then feel free to explore the rest of the notebooks. Also checkout Arviz which uses Xarray as its data model.