how to add a dimension coordinates

971 views
Skip to first unread message

Ryan Yan

unread,
Sep 2, 2017, 8:56:03 PM9/2/17
to xarray
Hello everyone,
I am wondering if anyone happens to know how to change a 2-dimensional Xarray Dataset into a 3-dimensional Xarray Dataset by adding a specific time as a dimension coordinates. So far, I can only add the specific time as an extra coordinate. But, I am not sure how to make 'time' as an extra dimension coordinates. Please see the code below,

In [1]: ds_daily2
Out [1]:
<xarray.Dataset>
Dimensions:     (lat: 553, lon: 721)
Coordinates:
  * lat         (lat) float32 28.0208 27.9792 27.9375 27.8958 27.8542 ...
  * lon         (lon) float32 44.9792 45.0208 45.0625 45.1042 45.1458 ...
Data variables:
    CHL1_mean   (lat, lon) float64 nan nan nan nan nan nan nan nan nan nan ...
    CHL1_flags  (lat, lon) float64 9.0 9.0 9.0 9.0 9.0 9.0 9.0 9.0 9.0 9.0 ...
    CHL1_error  (lat, lon) float64 nan nan nan nan nan nan nan nan nan nan ...


In [2]: ds_daily2_new = ds_daily2.assign_coords(time=pd.to_datetime('20170711'))
          ds_daily2_new
Out [2]:
<xarray.Dataset>
Dimensions:     (lat: 553, lon: 721)
Coordinates:
  * lat         (lat) float32 28.0208 27.9792 27.9375 27.8958 27.8542 ...
  * lon         (lon) float32 44.9792 45.0208 45.0625 45.1042 45.1458 ...
    time        datetime64[ns] 2017-07-11
Data variables:
    CHL1_mean   (lat, lon) float64 nan nan nan nan nan nan nan nan nan nan ...
    CHL1_flags  (lat, lon) float64 9.0 9.0 9.0 9.0 9.0 9.0 9.0 9.0 9.0 9.0 ...
    CHL1_error  (lat, lon) float64 nan nan nan nan nan nan nan nan nan nan ...

It would be great if I can make the new Dataset ds_daily2_new to have a dimension (lat: 553, lon: 721, time 1). The reason that I am trying to do this is that eventually I would like to aggregate ds_daily2_new, ds_daily3_new, ds_daily4_new,... into one Dataset along the time dimension. I would gratefully appreciate it if there is any suggestions or comments on this.

Best regards,
Yan





Stephan Hoyer

unread,
Sep 3, 2017, 12:19:45 AM9/3/17
to xarray
You're looking for the expand_dims() method
--
You received this message because you are subscribed to the Google Groups "xarray" group.
To unsubscribe from this group and stop receiving emails from it, send an email to xarray+un...@googlegroups.com.
To post to this group, send email to xar...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/xarray/d9e19168-94e0-4892-bbd5-5ede1955d7b5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ryan Yan

unread,
Sep 4, 2017, 10:28:53 PM9/4/17
to xarray
Hi Stephan,
Thank you very much for your suggestion. Now, the 'time' is added as an extra dimension for the Dataset.
In [16]:
ds_daily2_new.expand_dims('time')
Out [16]:
<xarray.Dataset>
Dimensions:     (lat: 553, lon: 721, time: 1)
Coordinates:
  * lat         (lat) float32 28.0208 27.9792 27.9375 27.8958 27.8542 ...
  * lon         (lon) float32 44.9792 45.0208 45.0625 45.1042 45.1458 ...
  * time        (time) datetime64[ns] 2017-07-11
Data variables:
    CHL1_mean   (time, lat, lon) float64 nan nan nan nan nan nan nan nan nan ...
    CHL1_flags  (time, lat, lon) float64 9.0 9.0 9.0 9.0 9.0 9.0 9.0 9.0 9.0 ...
    CHL1_error  (time, lat, lon) float64 nan nan nan nan nan nan nan nan nan ...

Best regards,
Yan
Reply all
Reply to author
Forward
0 new messages