# xarray.Dataset.assign_coords

#### Dataset.assign_coords(coords=None, \*\*coords_kwargs)[[source]](https://github.com/pydata/xarray/blob/main/xarray/core/common.py#L527-L672)

Assign new coordinates to this object.

Returns a new object with all the original data in addition to the new
coordinates.

* **Parameters:**
  * **coords** ([mapping](https://docs.python.org/3/glossary.html#term-mapping) of `dim` to `coord`, *optional*) – A mapping whose keys are the names of the coordinates and values are the
    coordinates to assign. The mapping will generally be a dict or
    [`Coordinates`](xarray.Coordinates.html.md#xarray.Coordinates).
    * If a value is a standard data value — for example, a `DataArray`,
      scalar, or array — the data is simply assigned as a coordinate.
    * If a value is callable, it is called with this object as the only
      parameter, and the return value is used as new coordinate variables.
    * A coordinate can also be defined and attached to an existing dimension
      using a tuple with the first element the dimension name and the second
      element the values for this new coordinate.
  * **\*\*coords_kwargs** (**optional**) – The keyword arguments form of `coords`.
    One of `coords` or `coords_kwargs` must be provided.
* **Returns:**
  **assigned** (`same type as caller`) – A new object with the new coordinates in addition to the existing
  data.

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('78e63f25-3a79-47c1-ac1f-dd164685a694','02da80d8-75df-4e77-89cc-c70d9f4253bb','4b072aae-0a2a-49bd-a960-21bcc359e61d','../lite/tree/../notebooks/index.html?path=0c93bde3_9589_45a1_b548_419316709ecb.ipynb','None')">Try it in your browser!</button></div>

Convert DataArray longitude coordinates from 0-359 to -180-179:

```pycon
>>> da = xr.DataArray(
...     np.random.rand(4),
...     coords=[np.array([358, 359, 0, 1])],
...     dims="lon",
... )
>>> da
<xarray.DataArray (lon: 4)> Size: 32B
array([0.5488135 , 0.71518937, 0.60276338, 0.54488318])
Coordinates:
  * lon      (lon) int64 32B 358 359 0 1
>>> da.assign_coords(lon=(((da.lon + 180) % 360) - 180))
<xarray.DataArray (lon: 4)> Size: 32B
array([0.5488135 , 0.71518937, 0.60276338, 0.54488318])
Coordinates:
  * lon      (lon) int64 32B -2 -1 0 1
```

The function also accepts dictionary arguments:

```pycon
>>> da.assign_coords({"lon": (((da.lon + 180) % 360) - 180)})
<xarray.DataArray (lon: 4)> Size: 32B
array([0.5488135 , 0.71518937, 0.60276338, 0.54488318])
Coordinates:
  * lon      (lon) int64 32B -2 -1 0 1
```

New coordinate can also be attached to an existing dimension:

```pycon
>>> lon_2 = np.array([300, 289, 0, 1])
>>> da.assign_coords(lon_2=("lon", lon_2))
<xarray.DataArray (lon: 4)> Size: 32B
array([0.5488135 , 0.71518937, 0.60276338, 0.54488318])
Coordinates:
  * lon      (lon) int64 32B 358 359 0 1
    lon_2    (lon) int64 32B 300 289 0 1
```

Note that the same result can also be obtained with a dict e.g.

```pycon
>>> _ = da.assign_coords({"lon_2": ("lon", lon_2)})
```

Note the same method applies to Dataset objects.

Convert Dataset longitude coordinates from 0-359 to -180-179:

```pycon
>>> temperature = np.linspace(20, 32, num=16).reshape(2, 2, 4)
>>> precipitation = 2 * np.identity(4).reshape(2, 2, 4)
>>> ds = xr.Dataset(
...     data_vars=dict(
...         temperature=(["x", "y", "time"], temperature),
...         precipitation=(["x", "y", "time"], precipitation),
...     ),
...     coords=dict(
...         lon=(["x", "y"], [[260.17, 260.68], [260.21, 260.77]]),
...         lat=(["x", "y"], [[42.25, 42.21], [42.63, 42.59]]),
...         time=pd.date_range("2014-09-06", periods=4),
...         reference_time=pd.Timestamp("2014-09-05"),
...     ),
...     attrs=dict(description="Weather-related data"),
... )
>>> ds
<xarray.Dataset> Size: 360B
Dimensions:         (x: 2, y: 2, time: 4)
Coordinates:
    lon             (x, y) float64 32B 260.2 260.7 260.2 260.8
    lat             (x, y) float64 32B 42.25 42.21 42.63 42.59
  * time            (time) datetime64[us] 32B 2014-09-06 ... 2014-09-09
    reference_time  datetime64[us] 8B 2014-09-05
Dimensions without coordinates: x, y
Data variables:
    temperature     (x, y, time) float64 128B 20.0 20.8 21.6 ... 30.4 31.2 32.0
    precipitation   (x, y, time) float64 128B 2.0 0.0 0.0 0.0 ... 0.0 0.0 2.0
Attributes:
    description:  Weather-related data
>>> ds.assign_coords(lon=(((ds.lon + 180) % 360) - 180))
<xarray.Dataset> Size: 360B
Dimensions:         (x: 2, y: 2, time: 4)
Coordinates:
    lon             (x, y) float64 32B -99.83 -99.32 -99.79 -99.23
    lat             (x, y) float64 32B 42.25 42.21 42.63 42.59
  * time            (time) datetime64[us] 32B 2014-09-06 ... 2014-09-09
    reference_time  datetime64[us] 8B 2014-09-05
Dimensions without coordinates: x, y
Data variables:
    temperature     (x, y, time) float64 128B 20.0 20.8 21.6 ... 30.4 31.2 32.0
    precipitation   (x, y, time) float64 128B 2.0 0.0 0.0 0.0 ... 0.0 0.0 2.0
Attributes:
    description:  Weather-related data
```

<div id="4b072aae-0a2a-49bd-a960-21bcc359e61d" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('78e63f25-3a79-47c1-ac1f-dd164685a694','4b072aae-0a2a-49bd-a960-21bcc359e61d')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('78e63f25-3a79-47c1-ac1f-dd164685a694','4b072aae-0a2a-49bd-a960-21bcc359e61d')">Open In Tab</button></div><div id="02da80d8-75df-4e77-89cc-c70d9f4253bb" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>

#### SEE ALSO
[`Dataset.assign`](xarray.Dataset.assign.html.md#xarray.Dataset.assign), [`Dataset.swap_dims`](xarray.Dataset.swap_dims.html.md#xarray.Dataset.swap_dims), [`Dataset.set_coords`](xarray.Dataset.set_coords.html.md#xarray.Dataset.set_coords)
