简体   繁体   中英

Assign coordinate to dimension in xarray

Suppose I have the following DataSet:

>>> coords = {"coords": ("x", [10, 20, 30, 40])}
>>> dset = xr.Dataset(coords=coords)
>>> dset
<xarray.Dataset>
Dimensions:  (x: 4)
Coordinates:
    coords   (x) int64 10 20 30 40
Dimensions without coordinates: x
Data variables:
    *empty*

How can I modify the dataset such that xarray knows to assign 'coords' to the dimension 'x'? Ie I do not want 'x' to be a dimension without coordinates, the coordinates are supposed to be 'coords'.

I'd like to know how to achieve this in these two ways:

  1. By modifying how the dataset is created
  2. By post-hoc "fixing" the existing dataset

xarray has concepts of both dimensions and coordinates . Dimensions are the names assigned to each array axis. Coordinates define labels along the axis.

You've defined the coordinate coords , indexed by dimension x . The coords coordinate has labels [10, 20, 30, 40] along dimension x . You never define labels for dimension x though.

What you probably mean is to have a coordinate x which provides labels for dimension x :

In [2]: coords = {"x": ("x", [10, 20, 30, 40])}
   ...: ds = xr.Dataset(coords=coords)
   ...: ds
Out[2]:
<xarray.Dataset>
Dimensions:  (x: 4)
Coordinates:
  * x        (x) int64 10 20 30 40
Data variables:
    *empty*

See the xarray docs and thexarray tutorial sections on Data Structures for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM