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 5 commits
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
26 changes: 26 additions & 0 deletions xarray/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,32 @@ def rolling(self, min_periods=None, center=False, **windows):
Returns
-------
rolling : type of input argument

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

>>> da = xr.DataArray(np.linspace(0,11,num=12),
coords=[pd.date_range('15/12/1999',
Copy link
Member

Choose a reason for hiding this comment

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

Use ... to continue a line (notice that this is what the python interpreter does).

periods=12, freq=pd.DateOffset(months=1))],
dims='time')
>>> da
<xarray.DataArray (time: 12)>
array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11.])
Coordinates:
* time (time) datetime64[ns] 1999-12-15 2000-01-15 2000-02-15 ...
>>> da_avg = da.rolling(time=3).mean()
>>> da_avg
<xarray.DataArray (time: 12)>
array([ nan, nan, 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
Coordinates:
* time (time) datetime64[ns] 1999-12-15 2000-01-15 2000-02-15 ...
>>> da_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.

Either add a little more exposition here to make it clear why you're showing this or drop it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are you referring to the output of da.rolling(time=3).mean() or the da.rolling(time=3).mean().dropna('time') statement?
You originally said it may be worth showing the output before restructuring the result of rolling. I wasn't sure about adding a comment in the middle of an example so i've opted to expand the description of the example. I decided to cut out the da.rolling(time=3).mean() line as a believe the output of da.rolling(time=3).mean().dropna('time') is cleaner but I understand if you would prefer not to add another function in the example.

Copy link
Member

Choose a reason for hiding this comment

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

You can definitely add comments in the middle of examples (e.g., see "Example" for np.matmul). Just add line breaks before/after and don't preface those lines with >>>, e.g.,

Add two numbers:

>>> 1 + 1
2

Subtract two numbers:

>>> 1 - 1
0

Even though the output is not as clean, my preference is for showing "minimal units" in docstrings so it's entirely clear to users how a function works. That way they'll know to expect NaNs, for example.

So you can also include the example using dropna() (with a comment) but we should have the simpler version without it first.

>>> da_avg
<xarray.DataArray (time: 10)>
array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
Coordinates:
* time (time) datetime64[ns] 2000-02-15 2000-03-15 2000-04-15 ...
"""

return self._rolling_cls(self, min_periods=min_periods,
Expand Down