# xarray.cov

### xarray.cov(da_a, da_b, dim=None, ddof=1, weights=None)[[source]](https://github.com/pydata/xarray/blob/main/xarray/computation/computation.py#L41-L144)

Compute covariance between two DataArray objects along a shared dimension.

* **Parameters:**
  * **da_a** ([`DataArray`](xarray.DataArray.html.md#xarray.DataArray)) – Array to compute.
  * **da_b** ([`DataArray`](xarray.DataArray.html.md#xarray.DataArray)) – Array to compute.
  * **dim** ([`str`](https://docs.python.org/3/builtins/stdtypes.html#str), [iterable](https://docs.python.org/3/glossary.html#term-iterable) of [hashable](../user-guide/terminology.html.md#term-name), `"..."` or [`None`](https://docs.python.org/3/builtins/constants.html#None), *optional*) – The dimension along which the covariance will be computed
  * **ddof** ([`int`](https://docs.python.org/3/builtins/functions.html#int), *default*: `1`) – If ddof=1, covariance is normalized by N-1, giving an unbiased estimate,
    else normalization is by N.
  * **weights** ([`DataArray`](xarray.DataArray.html.md#xarray.DataArray), *optional*) – Array of weights.
* **Returns:**
  **covariance** ([`DataArray`](xarray.DataArray.html.md#xarray.DataArray))

#### SEE ALSO
[`pandas.Series.cov`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.cov.html#pandas.Series.cov)
: corresponding pandas function

[`xarray.corr`](xarray.corr.html.md#xarray.corr)
: respective function to calculate correlation

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('be0f5215-cd74-453e-bbd3-797b67f55648','1755d69f-4ee4-43c1-9971-24587983c11f','0b9b80ff-f237-4ecd-8e1f-7c709ba6ba99','../lite/tree/../notebooks/index.html?path=43475778_ec38_40f2_abc2_aaf16a7d01c0.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> from xarray import DataArray
>>> da_a = DataArray(
...     np.array([[1, 2, 3], [0.1, 0.2, 0.3], [3.2, 0.6, 1.8]]),
...     dims=("space", "time"),
...     coords=[
...         ("space", ["IA", "IL", "IN"]),
...         ("time", pd.date_range("2000-01-01", freq="1D", periods=3)),
...     ],
... )
>>> da_a
<xarray.DataArray (space: 3, time: 3)> Size: 72B
array([[1. , 2. , 3. ],
       [0.1, 0.2, 0.3],
       [3.2, 0.6, 1.8]])
Coordinates:
  * space    (space) <U2 24B 'IA' 'IL' 'IN'
  * time     (time) datetime64[us] 24B 2000-01-01 2000-01-02 2000-01-03
>>> da_b = DataArray(
...     np.array([[0.2, 0.4, 0.6], [15, 10, 5], [3.2, 0.6, 1.8]]),
...     dims=("space", "time"),
...     coords=[
...         ("space", ["IA", "IL", "IN"]),
...         ("time", pd.date_range("2000-01-01", freq="1D", periods=3)),
...     ],
... )
>>> da_b
<xarray.DataArray (space: 3, time: 3)> Size: 72B
array([[ 0.2,  0.4,  0.6],
       [15. , 10. ,  5. ],
       [ 3.2,  0.6,  1.8]])
Coordinates:
  * space    (space) <U2 24B 'IA' 'IL' 'IN'
  * time     (time) datetime64[us] 24B 2000-01-01 2000-01-02 2000-01-03
>>> xr.cov(da_a, da_b)
<xarray.DataArray ()> Size: 8B
array(-3.53055556)
>>> xr.cov(da_a, da_b, dim="time")
<xarray.DataArray (space: 3)> Size: 24B
array([ 0.2       , -0.5       ,  1.69333333])
Coordinates:
  * space    (space) <U2 24B 'IA' 'IL' 'IN'
>>> weights = DataArray(
...     [4, 2, 1],
...     dims=("space"),
...     coords=[
...         ("space", ["IA", "IL", "IN"]),
...     ],
... )
>>> weights
<xarray.DataArray (space: 3)> Size: 24B
array([4, 2, 1])
Coordinates:
  * space    (space) <U2 24B 'IA' 'IL' 'IN'
>>> xr.cov(da_a, da_b, dim="space", weights=weights)
<xarray.DataArray (time: 3)> Size: 24B
array([-4.69346939, -4.49632653, -3.37959184])
Coordinates:
  * time     (time) datetime64[us] 24B 2000-01-01 2000-01-02 2000-01-03
```

<div id="0b9b80ff-f237-4ecd-8e1f-7c709ba6ba99" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('be0f5215-cd74-453e-bbd3-797b67f55648','0b9b80ff-f237-4ecd-8e1f-7c709ba6ba99')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('be0f5215-cd74-453e-bbd3-797b67f55648','0b9b80ff-f237-4ecd-8e1f-7c709ba6ba99')">Open In Tab</button></div><div id="1755d69f-4ee4-43c1-9971-24587983c11f" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
