How to create an xarray dataset from scratch

This is just a code snippet to remind myself how to build an xarray dataset.

Note: this post has been updated with further examples including spatial datasets HERE.

I couldn’t find an example online which creates an xr dataset in one go. So… this code will create a dataset of multiple variables, each with:

  • data
  • dimensions (in this case time)
  • coordinates (in this case times)
  • attributes

Global attributes are also defined.

import numpy as np
import xarray as xr
import pandas as pd

# create dummy dataframe
times = pd.date_range(start='2000-01-01',freq='1H',periods=6)

# create dataset
ds = xr.Dataset({
    'SWdown': xr.DataArray(
                data   = np.random.random(6),   # enter data here
                dims   = ['time'],
                coords = {'time': times},
                attrs  = {
                    '_FillValue': -999.9,
                    'units'     : 'W/m2'
                    }
                ),
    'LWdown': xr.DataArray(
                data   = np.random.random(6),   # enter data here
                dims   = ['time'],
                coords = {'time': times},
                attrs  = {
                    '_FillValue': -999.9,
                    'units'     : 'W/m2'
                    }
                )
            },
        attrs = {'example_attr': 'this is a global attribute'}
    )

The dataset can be inspected simply with:

ds

<xarray.Dataset>
Dimensions:  (time: 6)
Coordinates:
  * time     (time) datetime64[ns] 2000-01-01 ... 2000-01-01T05:00:00
Data variables:
    SWdown   (time) float64 0.1585 0.3689 0.8977 0.8216 0.6999 0.8178
    LWdown   (time) float64 0.895 0.3526 0.9385 0.2799 0.3736 0.3597
Attributes:
    glob_attr:  this is a global attribute

Embedded datasets are inspected with:

ds.SWdown

<xarray.DataArray 'SWdown' (time: 6)>
array([0.158523, 0.368877, 0.897713, 0.821555, 0.699896, 0.817798])
Coordinates:
  * time     (time) datetime64[ns] 2000-01-01 ... 2000-01-01T05:00:00
Attributes:
    _FillValue:  -999.9
    units:       W/m2

Share Comments
comments powered by Disqus