Skip to content

DOC: Fixed examples in pandas/core/window #33266

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 4 commits into from
Apr 4, 2020
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@ if [[ -z "$CHECK" || "$CHECK" == "doctests" ]]; then
pytest -q --doctest-modules pandas/core/tools/
RET=$(($RET + $?)) ; echo $MSG "DONE"

MSG='Doctests window' ; echo $MSG
pytest -q --doctest-modules pandas/core/window/
RET=$(($RET + $?)) ; echo $MSG "DONE"

MSG='Doctests tseries' ; echo $MSG
pytest -q --doctest-modules pandas/tseries/
RET=$(($RET + $?)) ; echo $MSG "DONE"
Expand Down
35 changes: 13 additions & 22 deletions pandas/core/window/ewm.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,33 +167,24 @@ def _constructor(self):
"""
Examples
--------

>>> df = pd.DataFrame(np.random.randn(10, 3), columns=['A', 'B', 'C'])
>>> df = pd.DataFrame(
... {
... "A": [0.95117561, 0.632442, 0.72290796],
Copy link
Member

Choose a reason for hiding this comment

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

Just a suggestion, I personally find these example easier to digest with "simpler numbers" like [1, 2, 3], [4, 5, 6]

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh OK, no problem. I'll fix that, was just avoiding random output in the docs.

... "B": [0.3740966, -1.48032596, -0.47270373],
... "C": [0.39592762, 0.73835624, -0.06817024],
... }
... )
>>> df
A B C
0 -2.385977 -0.102758 0.438822
1 -1.004295 0.905829 -0.954544
2 0.735167 -0.165272 -1.619346
3 -0.702657 -1.340923 -0.706334
4 -0.246845 0.211596 -0.901819
5 2.463718 3.157577 -1.380906
6 -1.142255 2.340594 -0.039875
7 1.396598 -1.647453 1.677227
8 -0.543425 1.761277 -0.220481
9 -0.640505 0.289374 -1.550670
0 0.951176 0.374097 0.395928
1 0.632442 -1.480326 0.738356
2 0.722908 -0.472704 -0.068170

>>> df.ewm(alpha=0.5).mean()
A B C
0 -2.385977 -0.102758 0.438822
1 -1.464856 0.569633 -0.490089
2 -0.207700 0.149687 -1.135379
3 -0.471677 -0.645305 -0.906555
4 -0.355635 -0.203033 -0.904111
5 1.076417 1.503943 -1.146293
6 -0.041654 1.925562 -0.588728
7 0.680292 0.132049 0.548693
8 0.067236 0.948257 0.163353
9 -0.286980 0.618493 -0.694496
0 0.951176 0.374097 0.395928
1 0.738687 -0.862185 0.624213
2 0.729670 -0.639624 0.228566
"""
)

Expand Down
38 changes: 15 additions & 23 deletions pandas/core/window/expanding.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class Expanding(_Rolling_and_Expanding):

Examples
--------
>>> df = pd.DataFrame({'B': [0, 1, 2, np.nan, 4]})
>>> df = pd.DataFrame({"B": [0, 1, 2, np.nan, 4]})
>>> df
B
0 0.0
1 1.0
Expand Down Expand Up @@ -98,33 +99,24 @@ def _get_window(self, other=None, **kwargs):
"""
Examples
--------

>>> df = pd.DataFrame(np.random.randn(10, 3), columns=['A', 'B', 'C'])
>>> df = pd.DataFrame(
... {
... "A": [0.95117561, 0.632442, 0.72290796],
... "B": [0.3740966, -1.48032596, -0.47270373],
... "C": [0.39592762, 0.73835624, -0.06817024],
... }
... )
>>> df
A B C
0 -2.385977 -0.102758 0.438822
1 -1.004295 0.905829 -0.954544
2 0.735167 -0.165272 -1.619346
3 -0.702657 -1.340923 -0.706334
4 -0.246845 0.211596 -0.901819
5 2.463718 3.157577 -1.380906
6 -1.142255 2.340594 -0.039875
7 1.396598 -1.647453 1.677227
8 -0.543425 1.761277 -0.220481
9 -0.640505 0.289374 -1.550670
0 0.951176 0.374097 0.395928
1 0.632442 -1.480326 0.738356
2 0.722908 -0.472704 -0.068170

>>> df.ewm(alpha=0.5).mean()
A B C
0 -2.385977 -0.102758 0.438822
1 -1.464856 0.569633 -0.490089
2 -0.207700 0.149687 -1.135379
3 -0.471677 -0.645305 -0.906555
4 -0.355635 -0.203033 -0.904111
5 1.076417 1.503943 -1.146293
6 -0.041654 1.925562 -0.588728
7 0.680292 0.132049 0.548693
8 0.067236 0.948257 0.163353
9 -0.286980 0.618493 -0.694496
0 0.951176 0.374097 0.395928
1 0.738687 -0.862185 0.624213
2 0.729670 -0.639624 0.228566
"""
)

Expand Down
85 changes: 30 additions & 55 deletions pandas/core/window/rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -1039,33 +1039,24 @@ def _get_window(
"""
Examples
--------

>>> df = pd.DataFrame(np.random.randn(10, 3), columns=['A', 'B', 'C'])
>>> df = pd.DataFrame(
... {
... "A": [0.95117561, 0.632442, 0.72290796],
... "B": [0.3740966, -1.48032596, -0.47270373],
... "C": [0.39592762, 0.73835624, -0.06817024],
... }
... )
>>> df
A B C
0 -2.385977 -0.102758 0.438822
1 -1.004295 0.905829 -0.954544
2 0.735167 -0.165272 -1.619346
3 -0.702657 -1.340923 -0.706334
4 -0.246845 0.211596 -0.901819
5 2.463718 3.157577 -1.380906
6 -1.142255 2.340594 -0.039875
7 1.396598 -1.647453 1.677227
8 -0.543425 1.761277 -0.220481
9 -0.640505 0.289374 -1.550670

>>> df.rolling(3, win_type='boxcar').agg('mean')
0 0.951176 0.374097 0.395928
1 0.632442 -1.480326 0.738356
2 0.722908 -0.472704 -0.068170

>>> df.rolling(3, win_type="boxcar").agg("mean")
A B C
0 NaN NaN NaN
1 NaN NaN NaN
2 -0.885035 0.212600 -0.711689
3 -0.323928 -0.200122 -1.093408
4 -0.071445 -0.431533 -1.075833
5 0.504739 0.676083 -0.996353
6 0.358206 1.903256 -0.774200
7 0.906020 1.283573 0.085482
8 -0.096361 0.818139 0.472290
9 0.070889 0.134399 -0.031308
2 0.768842 -0.526311 0.355371
"""
)

Expand Down Expand Up @@ -1904,46 +1895,30 @@ def _validate_freq(self):
"""
Examples
--------

>>> df = pd.DataFrame(np.random.randn(10, 3), columns=['A', 'B', 'C'])
>>> df = pd.DataFrame(
... {
... "A": [0.95117561, 0.632442, 0.72290796],
... "B": [0.3740966, -1.48032596, -0.47270373],
... "C": [0.39592762, 0.73835624, -0.06817024],
... }
... )
>>> df
A B C
0 -2.385977 -0.102758 0.438822
1 -1.004295 0.905829 -0.954544
2 0.735167 -0.165272 -1.619346
3 -0.702657 -1.340923 -0.706334
4 -0.246845 0.211596 -0.901819
5 2.463718 3.157577 -1.380906
6 -1.142255 2.340594 -0.039875
7 1.396598 -1.647453 1.677227
8 -0.543425 1.761277 -0.220481
9 -0.640505 0.289374 -1.550670
0 0.951176 0.374097 0.395928
1 0.632442 -1.480326 0.738356
2 0.722908 -0.472704 -0.068170

>>> df.rolling(3).sum()
>>> df.rolling(2).sum()
A B C
0 NaN NaN NaN
1 NaN NaN NaN
2 -2.655105 0.637799 -2.135068
3 -0.971785 -0.600366 -3.280224
4 -0.214334 -1.294599 -3.227500
5 1.514216 2.028250 -2.989060
6 1.074618 5.709767 -2.322600
7 2.718061 3.850718 0.256446
8 -0.289082 2.454418 1.416871
9 0.212668 0.403198 -0.093924

>>> df.rolling(3).agg({'A':'sum', 'B':'min'})
1 1.583618 -1.106229 1.134284
2 1.355350 -1.953030 0.670186

>>> df.rolling(2).agg({"A": "sum", "B": "min"})
A B
0 NaN NaN
1 NaN NaN
2 -2.655105 -0.165272
3 -0.971785 -1.340923
4 -0.214334 -1.340923
5 1.514216 -1.340923
6 1.074618 0.211596
7 2.718061 -1.647453
8 -0.289082 -1.647453
9 0.212668 -1.647453
1 1.583618 -1.480326
2 1.355350 -1.480326
"""
)

Expand Down