Skip to content

Commit ab60392

Browse files
authored
DOC: Fixed examples in pandas/core/window (#33266)
1 parent 3c843d6 commit ab60392

File tree

4 files changed

+49
-111
lines changed

4 files changed

+49
-111
lines changed

ci/code_checks.sh

+4
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,10 @@ if [[ -z "$CHECK" || "$CHECK" == "doctests" ]]; then
326326
pytest -q --doctest-modules pandas/core/tools/
327327
RET=$(($RET + $?)) ; echo $MSG "DONE"
328328

329+
MSG='Doctests window' ; echo $MSG
330+
pytest -q --doctest-modules pandas/core/window/
331+
RET=$(($RET + $?)) ; echo $MSG "DONE"
332+
329333
MSG='Doctests tseries' ; echo $MSG
330334
pytest -q --doctest-modules pandas/tseries/
331335
RET=$(($RET + $?)) ; echo $MSG "DONE"

pandas/core/window/ewm.py

+8-23
Original file line numberDiff line numberDiff line change
@@ -167,33 +167,18 @@ def _constructor(self):
167167
"""
168168
Examples
169169
--------
170-
171-
>>> df = pd.DataFrame(np.random.randn(10, 3), columns=['A', 'B', 'C'])
170+
>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9]})
172171
>>> df
173-
A B C
174-
0 -2.385977 -0.102758 0.438822
175-
1 -1.004295 0.905829 -0.954544
176-
2 0.735167 -0.165272 -1.619346
177-
3 -0.702657 -1.340923 -0.706334
178-
4 -0.246845 0.211596 -0.901819
179-
5 2.463718 3.157577 -1.380906
180-
6 -1.142255 2.340594 -0.039875
181-
7 1.396598 -1.647453 1.677227
182-
8 -0.543425 1.761277 -0.220481
183-
9 -0.640505 0.289374 -1.550670
172+
A B C
173+
0 1 4 7
174+
1 2 5 8
175+
2 3 6 9
184176
185177
>>> df.ewm(alpha=0.5).mean()
186178
A B C
187-
0 -2.385977 -0.102758 0.438822
188-
1 -1.464856 0.569633 -0.490089
189-
2 -0.207700 0.149687 -1.135379
190-
3 -0.471677 -0.645305 -0.906555
191-
4 -0.355635 -0.203033 -0.904111
192-
5 1.076417 1.503943 -1.146293
193-
6 -0.041654 1.925562 -0.588728
194-
7 0.680292 0.132049 0.548693
195-
8 0.067236 0.948257 0.163353
196-
9 -0.286980 0.618493 -0.694496
179+
0 1.000000 4.000000 7.000000
180+
1 1.666667 4.666667 7.666667
181+
2 2.428571 5.428571 8.428571
197182
"""
198183
)
199184

pandas/core/window/expanding.py

+10-24
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ class Expanding(_Rolling_and_Expanding):
3737
3838
Examples
3939
--------
40-
>>> df = pd.DataFrame({'B': [0, 1, 2, np.nan, 4]})
40+
>>> df = pd.DataFrame({"B": [0, 1, 2, np.nan, 4]})
41+
>>> df
4142
B
4243
0 0.0
4344
1 1.0
@@ -98,33 +99,18 @@ def _get_window(self, other=None, **kwargs):
9899
"""
99100
Examples
100101
--------
101-
102-
>>> df = pd.DataFrame(np.random.randn(10, 3), columns=['A', 'B', 'C'])
102+
>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9]})
103103
>>> df
104-
A B C
105-
0 -2.385977 -0.102758 0.438822
106-
1 -1.004295 0.905829 -0.954544
107-
2 0.735167 -0.165272 -1.619346
108-
3 -0.702657 -1.340923 -0.706334
109-
4 -0.246845 0.211596 -0.901819
110-
5 2.463718 3.157577 -1.380906
111-
6 -1.142255 2.340594 -0.039875
112-
7 1.396598 -1.647453 1.677227
113-
8 -0.543425 1.761277 -0.220481
114-
9 -0.640505 0.289374 -1.550670
104+
A B C
105+
0 1 4 7
106+
1 2 5 8
107+
2 3 6 9
115108
116109
>>> df.ewm(alpha=0.5).mean()
117110
A B C
118-
0 -2.385977 -0.102758 0.438822
119-
1 -1.464856 0.569633 -0.490089
120-
2 -0.207700 0.149687 -1.135379
121-
3 -0.471677 -0.645305 -0.906555
122-
4 -0.355635 -0.203033 -0.904111
123-
5 1.076417 1.503943 -1.146293
124-
6 -0.041654 1.925562 -0.588728
125-
7 0.680292 0.132049 0.548693
126-
8 0.067236 0.948257 0.163353
127-
9 -0.286980 0.618493 -0.694496
111+
0 1.000000 4.000000 7.000000
112+
1 1.666667 4.666667 7.666667
113+
2 2.428571 5.428571 8.428571
128114
"""
129115
)
130116

pandas/core/window/rolling.py

+27-64
Original file line numberDiff line numberDiff line change
@@ -1039,33 +1039,18 @@ def _get_window(
10391039
"""
10401040
Examples
10411041
--------
1042-
1043-
>>> df = pd.DataFrame(np.random.randn(10, 3), columns=['A', 'B', 'C'])
1042+
>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9]})
10441043
>>> df
1045-
A B C
1046-
0 -2.385977 -0.102758 0.438822
1047-
1 -1.004295 0.905829 -0.954544
1048-
2 0.735167 -0.165272 -1.619346
1049-
3 -0.702657 -1.340923 -0.706334
1050-
4 -0.246845 0.211596 -0.901819
1051-
5 2.463718 3.157577 -1.380906
1052-
6 -1.142255 2.340594 -0.039875
1053-
7 1.396598 -1.647453 1.677227
1054-
8 -0.543425 1.761277 -0.220481
1055-
9 -0.640505 0.289374 -1.550670
1056-
1057-
>>> df.rolling(3, win_type='boxcar').agg('mean')
1058-
A B C
1059-
0 NaN NaN NaN
1060-
1 NaN NaN NaN
1061-
2 -0.885035 0.212600 -0.711689
1062-
3 -0.323928 -0.200122 -1.093408
1063-
4 -0.071445 -0.431533 -1.075833
1064-
5 0.504739 0.676083 -0.996353
1065-
6 0.358206 1.903256 -0.774200
1066-
7 0.906020 1.283573 0.085482
1067-
8 -0.096361 0.818139 0.472290
1068-
9 0.070889 0.134399 -0.031308
1044+
A B C
1045+
0 1 4 7
1046+
1 2 5 8
1047+
2 3 6 9
1048+
1049+
>>> df.rolling(2, win_type="boxcar").agg("mean")
1050+
A B C
1051+
0 NaN NaN NaN
1052+
1 1.5 4.5 7.5
1053+
2 2.5 5.5 8.5
10691054
"""
10701055
)
10711056

@@ -1904,46 +1889,24 @@ def _validate_freq(self):
19041889
"""
19051890
Examples
19061891
--------
1907-
1908-
>>> df = pd.DataFrame(np.random.randn(10, 3), columns=['A', 'B', 'C'])
1892+
>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6], "C": [7, 8, 9]})
19091893
>>> df
1910-
A B C
1911-
0 -2.385977 -0.102758 0.438822
1912-
1 -1.004295 0.905829 -0.954544
1913-
2 0.735167 -0.165272 -1.619346
1914-
3 -0.702657 -1.340923 -0.706334
1915-
4 -0.246845 0.211596 -0.901819
1916-
5 2.463718 3.157577 -1.380906
1917-
6 -1.142255 2.340594 -0.039875
1918-
7 1.396598 -1.647453 1.677227
1919-
8 -0.543425 1.761277 -0.220481
1920-
9 -0.640505 0.289374 -1.550670
1894+
A B C
1895+
0 1 4 7
1896+
1 2 5 8
1897+
2 3 6 9
19211898
1922-
>>> df.rolling(3).sum()
1923-
A B C
1924-
0 NaN NaN NaN
1925-
1 NaN NaN NaN
1926-
2 -2.655105 0.637799 -2.135068
1927-
3 -0.971785 -0.600366 -3.280224
1928-
4 -0.214334 -1.294599 -3.227500
1929-
5 1.514216 2.028250 -2.989060
1930-
6 1.074618 5.709767 -2.322600
1931-
7 2.718061 3.850718 0.256446
1932-
8 -0.289082 2.454418 1.416871
1933-
9 0.212668 0.403198 -0.093924
1934-
1935-
>>> df.rolling(3).agg({'A':'sum', 'B':'min'})
1936-
A B
1937-
0 NaN NaN
1938-
1 NaN NaN
1939-
2 -2.655105 -0.165272
1940-
3 -0.971785 -1.340923
1941-
4 -0.214334 -1.340923
1942-
5 1.514216 -1.340923
1943-
6 1.074618 0.211596
1944-
7 2.718061 -1.647453
1945-
8 -0.289082 -1.647453
1946-
9 0.212668 -1.647453
1899+
>>> df.rolling(2).sum()
1900+
A B C
1901+
0 NaN NaN NaN
1902+
1 3.0 9.0 15.0
1903+
2 5.0 11.0 17.0
1904+
1905+
>>> df.rolling(2).agg({"A": "sum", "B": "min"})
1906+
A B
1907+
0 NaN NaN
1908+
1 3.0 4.0
1909+
2 5.0 5.0
19471910
"""
19481911
)
19491912

0 commit comments

Comments
 (0)