home / github / issues

Menu
  • GraphQL API
  • Search all tables

issues: 543825272

This data as json

id node_id number title user state locked assignee milestone comments created_at updated_at closed_at author_association active_lock_reason draft pull_request body reactions performed_via_github_app state_reason repo type
543825272 MDU6SXNzdWU1NDM4MjUyNzI= 3656 apply rolling window on function 44284270 closed 0     5 2019-12-30T10:16:27Z 2021-02-12T00:05:52Z 2021-02-12T00:05:51Z NONE      

I am looking for how to apply xarray rolling window to this function. I have really searched a lot but my effort is still futile..

My data is xarray data of shape (time, lon, lat) (323x180x360) respectively

```python def get_grps(s,axis=0, thresh=-1, Nmin=2): """ Nmin : int > 0 Min number of consecutive values below threshold. """ s = pd.Series(s) m = np.logical_and.reduce([s.shift(-i).le(thresh) for i in range(Nmin)]) if Nmin > 1: m = pd.Series(m, index=s.index).replace({False: np.NaN}).ffill(limit=Nmin-1).fillna(False) else: m = pd.Series(m, index=s.index)

# Form consecutive groups
gps = m.ne(m.shift(1)).cumsum().where(m)

# Return None if no groups, else the aggregations
if gps.isnull().all():
    return [0]
else:
    d = s.groupby(gps).agg([list, sum, 'size']).reset_index(drop=True)

    data = len((d['sum']))  ### returning frequency

    data = np.array(data)
    return (data)

```

I tried this

python spi = xr.DataArray(dims=("time", "lon", "lat"), data=np.random.randn(324, 180, 360)) spi.rolling(time=59).reduce(get_grps)

I get this error

python Traceback (most recent call last): File "<input>", line 1, in <module> File "/Users/mada0007/anaconda3/envs/RESEARCH_QUESTION_TWO/lib/python3.7/site-packages/xarray/core/rolling.py", line 287, in reduce result = windows.reduce(func, dim=rolling_dim, **kwargs) File "/Users/mada0007/anaconda3/envs/RESEARCH_QUESTION_TWO/lib/python3.7/site-packages/xarray/core/dataarray.py", line 2216, in reduce var = self.variable.reduce(func, dim, axis, keep_attrs, keepdims, **kwargs) File "/Users/mada0007/anaconda3/envs/RESEARCH_QUESTION_TWO/lib/python3.7/site-packages/xarray/core/variable.py", line 1529, in reduce data = func(input_data, axis=axis, **kwargs) File "<input>", line 7, in get_grps File "/Users/mada0007/anaconda3/envs/RESEARCH_QUESTION_TWO/lib/python3.7/site-packages/pandas/core/series.py", line 314, in __init__ data = sanitize_array(data, index, dtype, copy, raise_cast_failure=True) File "/Users/mada0007/anaconda3/envs/RESEARCH_QUESTION_TWO/lib/python3.7/site-packages/pandas/core/internals/construction.py", line 730, in sanitize_array raise Exception("Data must be 1-dimensional") Exception: Data must be 1-dimensional

How can I rectify this?

Thanks

{
    "url": "https://api.github.com/repos/pydata/xarray/issues/3656/reactions",
    "total_count": 0,
    "+1": 0,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
  completed 13221727 issue

Links from other tables

  • 2 rows from issues_id in issues_labels
  • 5 rows from issue in issue_comments
Powered by Datasette · Queries took 0.736ms · About: xarray-datasette