xarray.DataArray.assign_coords¶
-
DataArray.
assign_coords
(**kwargs)¶ Assign new coordinates to this object.
Returns a new object with all the original data in addition to the new coordinates.
Parameters: - kwargs : keyword, value pairs
keywords are the variables names. If the values are callable, they are computed on this object and assigned to new coordinate variables. If the values are not callable, (e.g. a DataArray, scalar, or array), they are simply assigned.
Returns: - assigned : same type as caller
A new object with the new coordinates in addition to the existing data.
See also
Notes
Since
kwargs
is a dictionary, the order of your arguments may not be preserved, and so the order of the new variables is not well defined. Assigning multiple variables within the sameassign_coords
is possible, but you cannot reference other variables created within the sameassign_coords
call.Examples
Convert longitude coordinates from 0-359 to -180-179:
>>> da = xr.DataArray(np.random.rand(4), ... coords=[np.array([358, 359, 0, 1])], ... dims='lon') >>> da <xarray.DataArray (lon: 4)> array([0.28298 , 0.667347, 0.657938, 0.177683]) Coordinates: * lon (lon) int64 358 359 0 1 >>> da.assign_coords(lon=(((da.lon + 180) % 360) - 180)) <xarray.DataArray (lon: 4)> array([0.28298 , 0.667347, 0.657938, 0.177683]) Coordinates: * lon (lon) int64 -2 -1 0 1