14

I have a DataArray object named rio.

In [59]: rio
Out[59]: 
<xarray.DataArray (band: 1, y: 106, x: 193)>
array([[[0, 0, ..., 0, 0],
        [0, 0, ..., 0, 0],
        ...,
        [0, 0, ..., 0, 0],
        [0, 0, ..., 0, 0]]], dtype=uint8)
Coordinates:
  * band     (band) int32 1
  * y        (y) float64 -33.9 -33.95 -34.0 -34.05 ... -39.05 -39.1 -39.15 -39.2
  * x        (x) float64 140.8 140.8 140.9 140.9 ... 149.9 149.9 150.0 150.0
Attributes:
    transform:   (0.04791259799999997, 0.0, 140.776046753, 0.0, -0.0504760740...
    crs:         +init=epsg:4326
    res:         (0.04791259799999997, 0.05047607400000004)
    is_tiled:    1
    nodatavals:  (255.0,)

I would like to rename y to latitude and x to longitude. So the resulted rio object will be:

In [59]: rio
Out[59]: 
<xarray.DataArray (band: 1, latitude: 106, longitude: 193)>
array([[[0, 0, ..., 0, 0],
        [0, 0, ..., 0, 0],
        ...,
        [0, 0, ..., 0, 0],
        [0, 0, ..., 0, 0]]], dtype=uint8)
Coordinates:
  * band     (band) int32 1
  * latitude         (latitude) float64 -33.9 -33.95 -34.0 -34.05 ... -39.05 -39.1 -39.15 -39.2
  * longitude        (longitude) float64 140.8 140.8 140.9 140.9 ... 149.9 149.9 150.0 150.0
Attributes:
    transform:   (0.04791259799999997, 0.0, 140.776046753, 0.0, -0.0504760740...
    crs:         +init=epsg:4326
    res:         (0.04791259799999997, 0.05047607400000004)
    is_tiled:    1
    nodatavals:  (255.0,)

I need help on what methods I should use on the DataArray class?

2 Answers 2

28

I figured it out:

   rio = rio.rename({'x': 'longitude','y': 'latitude'})
2

Just to add to the answer for others coming here from google. When you rename the dimensions, there's a new DataArray returned. And you have to assign that back to the old name. Meaning you should do rio = rio.rename({'x': 'longitude','y': 'latitude'}). That wasn't obvious to me, just renaming it isn't enough.

Your Answer

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

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