Skip to content

DOC: Added examples to rolling.py #1574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Sep 21, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions xarray/core/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,80 @@ def __init__(self, obj, min_periods=None, center=False, **windows):
Returns
-------
rolling : type of input argument

Examples
-------
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have one more -.

Create rolling seasonal average of monthly data e.g. DJF, JFM, ..., SON

>>> lat = np.linspace(-90, 90, num=181)
>>> lon = np.linspace(0, 359, num=360)
>>> time = pd.date_range('15/12/1999',
periods=12, freq=pd.DateOffset(months=1))
>>> a = np.repeat(np.repeat(np.linspace(0,11,num=12)[:, np.newaxis],
len(lat), axis=1)[:, :, np.newaxis], len(lon), axis=2)
>>> da = xr.DataArray(a, coords=[time, lat, lon], dims=['time','lat','lon'])
>>> da
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a fine example, but it takes a lot of space to show it because of the unused lat/lon dimensions. Maybe it would be better to drop those dimensions and make the array only go along time?

<xarray.DataArray (time: 12, lat: 181, lon: 360)>
array([[[ 0., 0., ..., 0., 0.],
[ 0., 0., ..., 0., 0.],
...,
[ 0., 0., ..., 0., 0.],
[ 0., 0., ..., 0., 0.]],

[[ 1., 1., ..., 1., 1.],
[ 1., 1., ..., 1., 1.],
...,
[ 1., 1., ..., 1., 1.],
[ 1., 1., ..., 1., 1.]],

...,
[[ 10., 10., ..., 10., 10.],
[ 10., 10., ..., 10., 10.],
...,
[ 10., 10., ..., 10., 10.],
[ 10., 10., ..., 10., 10.]],

[[ 11., 11., ..., 11., 11.],
[ 11., 11., ..., 11., 11.],
...,
[ 11., 11., ..., 11., 11.],
[ 11., 11., ..., 11., 11.]]])
Coordinates:
* time (time) datetime64[ns] 1999-12-15 2000-01-15 2000-02-15 ...
* lat (lat) float64 -90.0 -89.0 -88.0 -87.0 -86.0 -85.0 -84.0 -83.0 ...
* lon (lon) float64 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 ...
>>> da_seasonal_avg = da.rolling(time=3).mean().dropna('time')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For examples in docstrings, we usually try to keep things as simple as possible. The dropna() and reassignment below are good tips for restructuring the result of rolling, but we should show the intermediate output first, e.g., by directly printing da.rolling(time=3).mean().

>>> da_seasonal_avg['time'] = time[1:len(time)-1]
>>> da_seasonal_avg
<xarray.DataArray (time: 10, lat: 181, lon: 360)>
array([[[ 1., 1., ..., 1., 1.],
[ 1., 1., ..., 1., 1.],
...,
[ 1., 1., ..., 1., 1.],
[ 1., 1., ..., 1., 1.]],

[[ 2., 2., ..., 2., 2.],
[ 2., 2., ..., 2., 2.],
...,
[ 2., 2., ..., 2., 2.],
[ 2., 2., ..., 2., 2.]],

...,
[[ 9., 9., ..., 9., 9.],
[ 9., 9., ..., 9., 9.],
...,
[ 9., 9., ..., 9., 9.],
[ 9., 9., ..., 9., 9.]],

[[ 10., 10., ..., 10., 10.],
[ 10., 10., ..., 10., 10.],
...,
[ 10., 10., ..., 10., 10.],
[ 10., 10., ..., 10., 10.]]])
Coordinates:
* time (time) datetime64[ns] 2000-01-15 2000-02-15 2000-03-15 ...
* lat (lat) float64 -90.0 -89.0 -88.0 -87.0 -86.0 -85.0 -84.0 -83.0 ...
* lon (lon) float64 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0 ...
"""

if (has_bottleneck and
Expand Down