Skip to content Skip to sidebar Skip to footer

Add A 'time' Dimension To Xarray Dataset And Assign Coordinates From Another Dataset To It

I have a Dataset object (imported from a netCDF file through xarray.open_dataset) named ds. It contains a variable named variable1 and latitude and longitude dimensions. >>&g

Solution 1:

There is an appropriate usage of expand_dims for you:

>>> dst = ds.expand_dims(time=time_da)
>>> dst
<xarray.Dataset>
Dimensions:    (latitude: 681, longitude: 841, time: 365)
Coordinates:
  * time       (time) datetime64[ns] 2017-01-01 2017-01-02 ... 2017-12-31
  * latitude   (latitude) int64 0 1 2 3 4 5 6 7 ... 674 675 676 677 678 679 680
  * longitude  (longitude) int64 0 1 2 3 4 5 6 7 ... 834 835 836 837 838 839 840
Data variables:
    variable   (time, latitude, longitude) float64 0.03968 2.156 ... -1.752

Checking that variable is the same at each timestep:

>>> np.all(np.diff(dst["variable"], axis=0) == 0)
True

Post a Comment for "Add A 'time' Dimension To Xarray Dataset And Assign Coordinates From Another Dataset To It"