home / github / issue_comments

Menu
  • GraphQL API
  • Search all tables

issue_comments: 747376047

This data as json

html_url issue_url id node_id user created_at updated_at author_association body reactions performed_via_github_app issue
https://github.com/pydata/xarray/issues/2007#issuecomment-747376047 https://api.github.com/repos/pydata/xarray/issues/2007 747376047 MDEyOklzc3VlQ29tbWVudDc0NzM3NjA0Nw== 10194086 2020-12-17T11:14:33Z 2020-12-17T16:02:25Z MEMBER

I just need to find the three warmest consecutive months from a temperature dataset for my work, so I thought I add a complete example. First, create an example dataset with monthly temperature:

```python import xarray as xr import numpy as np import pandas as pd

time = pd.date_range("2000", periods=12 * 30, freq="M") temp = np.sin((time.month - 5) / 6 * np.pi) + np.random.randn(*time.shape) * 0.3 da = xr.DataArray(temp, dims=["time"], coords=dict(time=time)) print(da) ```

python <xarray.DataArray (time: 360)> array([-0.676731, -0.812742, -1.367547, ..., 0.186731, 0.237676, -0.343879]) Coordinates: * time (time) datetime64[ns] 2000-01-31 2000-02-29 ... 2029-12-31

Currently we can achieve this like:

```python n_months = 3

monthly = da.groupby("time.month").mean() padded = monthly.pad(month=n_months, mode="wrap") rolled = padded.rolling(center=True, month=n_months).mean(skipna=False) sliced = rolled.isel(month=slice(3, -3))

central_month = sliced.idxmax() ```

Implementing pad_mode in rolling would allow to do:

```python monthly = da.groupby("time.month").mean() rolled = monthly.rolling(center=True, month=n_months, pad_mode="wrap").mean(skipna=False)

central_month = rolled.idxmax() ```

{
    "total_count": 0,
    "+1": 0,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
  307783090
Powered by Datasette · Queries took 0.677ms · About: xarray-datasette