I have a netCDF which is loaded in xarray with a dimension named bands (it was originally an import via rioxarray of ENVI data), but actually, I want to be able to parse the data by time. I have tried this:

Data structure

And indeed is created a "time" coordinate in the dataset, but is kind of useless when I try to parse the data as such:

ds.sel(time="1982-01-31")['leafleaf_area_index_GLASS'].where(ds.leaf_area_index_GLASS > 20).plot()

Any suggestions on how can I make this time coordinate to work? I'm not sure if there is an import option with xarray in which I could map directly the band into a datetime coordinate.

  • 1
    Use .assign_coords to change the coordinate values of “band”. Then .rename “band” to “time” Mar 2 at 21:48

2 Answers 2


You created a new coordinate but it also created a new dimension instead of using the "band" dimension. @Bert Coever's suggestion should work. Also note that every operation creates a new dataset, therefore one needs to assign it back to the dataset:

ds= ds.assign_coords({"band" : pd.Series(pd.date_range ... ...)})
ds= ds.rename_dims({"band" : "time"})

Something like this should work:

time = pd.date_range("1982-01-01", periods=408, frequ="M")

The way it works is that you should specify to xarray what is the dimension to this new coordinate. Then you rename the coordinate and dimension to time.

Here below I give you my reproducible code snippet for my example

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

n = 408
ds = xr.Dataset(dict(param=("band", np.random.randn(n))), coords=dict(band=np.arange(n)))

time = pd.date_range("1982-01-01", periods=n, freq="M")
ds = ds.assign_coords(band=("band",time)).rename(band="time")


Which returns the following:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.