From 06c373821f81f1d4c52fc667105f220c482aa387 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dea=20Mar=C3=ADa=20L=C3=A9on?= Date: Mon, 3 Jul 2023 18:32:24 +0200 Subject: [PATCH 1/3] Expanding examples --- ci/code_checks.sh | 11 --- pandas/core/window/expanding.py | 159 +++++++++++++++++++++++++++++--- 2 files changed, 148 insertions(+), 22 deletions(-) diff --git a/ci/code_checks.sh b/ci/code_checks.sh index a67dc66b26d34..2c43bd794b798 100755 --- a/ci/code_checks.sh +++ b/ci/code_checks.sh @@ -118,17 +118,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then pandas.core.window.rolling.Window.sum \ pandas.core.window.rolling.Window.var \ pandas.core.window.rolling.Window.std \ - pandas.core.window.expanding.Expanding.count \ - pandas.core.window.expanding.Expanding.sum \ - pandas.core.window.expanding.Expanding.mean \ - pandas.core.window.expanding.Expanding.median \ - pandas.core.window.expanding.Expanding.min \ - pandas.core.window.expanding.Expanding.max \ - pandas.core.window.expanding.Expanding.corr \ - pandas.core.window.expanding.Expanding.cov \ - pandas.core.window.expanding.Expanding.skew \ - pandas.core.window.expanding.Expanding.apply \ - pandas.core.window.expanding.Expanding.quantile \ pandas.core.window.ewm.ExponentialMovingWindow.mean \ pandas.core.window.ewm.ExponentialMovingWindow.sum \ pandas.core.window.ewm.ExponentialMovingWindow.std \ diff --git a/pandas/core/window/expanding.py b/pandas/core/window/expanding.py index 19dd98851611f..85bfb82683158 100644 --- a/pandas/core/window/expanding.py +++ b/pandas/core/window/expanding.py @@ -183,7 +183,19 @@ def aggregate(self, func, *args, **kwargs): create_section_header("Returns"), template_returns, create_section_header("See Also"), - template_see_also[:-1], + template_see_also, + create_section_header("Examples"), + dedent( + """ + >>> ser = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']) + >>> ser.expanding().count() + a 1.0 + b 2.0 + c 3.0 + d 4.0 + dtype: float64 + """ + ).replace("\n", "", 1), window_method="expanding", aggregation_description="count of non NaN observations", agg_method="count", @@ -198,7 +210,19 @@ def count(self, numeric_only: bool = False): create_section_header("Returns"), template_returns, create_section_header("See Also"), - template_see_also[:-1], + template_see_also, + create_section_header("Examples"), + dedent( + """ + >>> ser = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']) + >>> ser.expanding().apply(lambda s: s.max() - 2 * s.min()) + a -1.0 + b 0.0 + c 1.0 + d 2.0 + dtype: float64 + """ + ).replace("\n", "", 1), window_method="expanding", aggregation_description="custom aggregation function", agg_method="apply", @@ -231,7 +255,19 @@ def apply( create_section_header("See Also"), template_see_also, create_section_header("Notes"), - numba_notes[:-1], + numba_notes, + create_section_header("Examples"), + dedent( + """ + >>> ser = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']) + >>> ser.expanding().sum() + a 1.0 + b 3.0 + c 6.0 + d 10.0 + dtype: float64 + """ + ).replace("\n", "", 1), window_method="expanding", aggregation_description="sum", agg_method="sum", @@ -258,7 +294,19 @@ def sum( create_section_header("See Also"), template_see_also, create_section_header("Notes"), - numba_notes[:-1], + numba_notes, + create_section_header("Examples"), + dedent( + """ + >>> ser = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']) + >>> ser.expanding().max() + a 1.0 + b 2.0 + c 3.0 + d 4.0 + dtype: float64 + """ + ).replace("\n", "", 1), window_method="expanding", aggregation_description="maximum", agg_method="max", @@ -285,7 +333,19 @@ def max( create_section_header("See Also"), template_see_also, create_section_header("Notes"), - numba_notes[:-1], + numba_notes, + create_section_header("Examples"), + dedent( + """ + >>> ser = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']) + >>> ser.expanding().min() + a 1.0 + b 1.0 + c 1.0 + d 1.0 + dtype: float64 + """ + ).replace("\n", "", 1), window_method="expanding", aggregation_description="minimum", agg_method="min", @@ -312,7 +372,19 @@ def min( create_section_header("See Also"), template_see_also, create_section_header("Notes"), - numba_notes[:-1], + numba_notes, + create_section_header("Examples"), + dedent( + """ + >>> ser = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']) + >>> ser.expanding().mean() + a 1.0 + b 1.5 + c 2.0 + d 2.5 + dtype: float64 + """ + ).replace("\n", "", 1), window_method="expanding", aggregation_description="mean", agg_method="mean", @@ -339,7 +411,19 @@ def mean( create_section_header("See Also"), template_see_also, create_section_header("Notes"), - numba_notes[:-1], + numba_notes, + create_section_header("Examples"), + dedent( + """ + >>> ser = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']) + >>> ser.expanding().median() + a 1.0 + b 1.5 + c 2.0 + d 2.5 + dtype: float64 + """ + ).replace("\n", "", 1), window_method="expanding", aggregation_description="median", agg_method="median", @@ -523,7 +607,20 @@ def sem(self, ddof: int = 1, numeric_only: bool = False): "scipy.stats.skew : Third moment of a probability density.\n", template_see_also, create_section_header("Notes"), - "A minimum of three periods is required for the rolling calculation.\n", + "A minimum of three periods is required for the rolling calculation.\n\n", + create_section_header("Examples"), + dedent( + """ + >>> ser = pd.Series([-1, 0, 2, -1, 2], index=['a', 'b', 'c', 'd', 'e']) + >>> ser.expanding().skew() + a NaN + b NaN + c 0.935220 + d 1.414214 + e 0.315356 + dtype: float64 + """ + ).replace("\n", "", 1), window_method="expanding", aggregation_description="unbiased skewness", agg_method="skew", @@ -597,7 +694,21 @@ def kurt(self, numeric_only: bool = False): create_section_header("Returns"), template_returns, create_section_header("See Also"), - template_see_also[:-1], + template_see_also, + create_section_header("Examples"), + dedent( + """ + >>> ser = pd.Series([1, 2, 3, 4, 5, 6], index=['a', 'b', 'c', 'd', 'e', 'f']) + >>> ser.expanding(min_periods=4).quantile(.25) + a NaN + b NaN + c NaN + d 1.75 + e 2.00 + f 2.25 + dtype: float64 + """ + ).replace("\n", "", 1), window_method="expanding", aggregation_description="quantile", agg_method="quantile", @@ -714,7 +825,20 @@ def rank( create_section_header("Returns"), template_returns, create_section_header("See Also"), - template_see_also[:-1], + template_see_also, + create_section_header("Examples"), + dedent( + """ + >>> ser1 = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']) + >>> ser2 = pd.Series([10, 11, 13, 16], index=['a', 'b', 'c', 'd']) + >>> ser1.expanding().cov(ser2) + a NaN + b 0.500000 + c 1.500000 + d 3.333333 + dtype: float64 + """ + ).replace("\n", "", 1), window_method="expanding", aggregation_description="sample covariance", agg_method="cov", @@ -782,7 +906,20 @@ def cov( columns on the second level. In the case of missing elements, only complete pairwise observations - will be used. + will be used.\n + """ + ), + create_section_header("Examples"), + dedent( + """ + >>> ser1 = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']) + >>> ser2 = pd.Series([10, 11, 13, 16], index=['a', 'b', 'c', 'd']) + >>> ser1.expanding().corr(ser2) + a NaN + b 1.000000 + c 0.981981 + d 0.975900 + dtype: float64 """ ).replace("\n", "", 1), window_method="expanding", From 5f286fd2db6affa123b681aa130628e5b483d109 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dea=20Mar=C3=ADa=20L=C3=A9on?= Date: Tue, 4 Jul 2023 17:17:01 +0200 Subject: [PATCH 2/3] change list for examples max, min --- pandas/core/window/expanding.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pandas/core/window/expanding.py b/pandas/core/window/expanding.py index 85bfb82683158..3a409e1a924c2 100644 --- a/pandas/core/window/expanding.py +++ b/pandas/core/window/expanding.py @@ -298,10 +298,10 @@ def sum( create_section_header("Examples"), dedent( """ - >>> ser = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']) + >>> ser = pd.Series([3, 2, 1, 4], index=['a', 'b', 'c', 'd']) >>> ser.expanding().max() - a 1.0 - b 2.0 + a 3.0 + b 3.0 c 3.0 d 4.0 dtype: float64 @@ -336,16 +336,16 @@ def max( numba_notes, create_section_header("Examples"), dedent( - """ - >>> ser = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']) + """\ + >>> ser = pd.Series([2, 3, 4, 1], index=['a', 'b', 'c', 'd']) >>> ser.expanding().min() - a 1.0 - b 1.0 - c 1.0 + a 2.0 + b 2.0 + c 2.0 d 1.0 dtype: float64 """ - ).replace("\n", "", 1), + ), window_method="expanding", aggregation_description="minimum", agg_method="min", From dfa0cd4e50bd18291998dcf93dc844ec7a133915 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dea=20Mar=C3=ADa=20L=C3=A9on?= Date: Tue, 4 Jul 2023 18:13:44 +0200 Subject: [PATCH 3/3] Remove replace() on Expanding examples --- pandas/core/window/expanding.py | 40 ++++++++++++++++----------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/pandas/core/window/expanding.py b/pandas/core/window/expanding.py index 3a409e1a924c2..ec4c23bfc5e49 100644 --- a/pandas/core/window/expanding.py +++ b/pandas/core/window/expanding.py @@ -186,7 +186,7 @@ def aggregate(self, func, *args, **kwargs): template_see_also, create_section_header("Examples"), dedent( - """ + """\ >>> ser = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']) >>> ser.expanding().count() a 1.0 @@ -195,7 +195,7 @@ def aggregate(self, func, *args, **kwargs): d 4.0 dtype: float64 """ - ).replace("\n", "", 1), + ), window_method="expanding", aggregation_description="count of non NaN observations", agg_method="count", @@ -213,7 +213,7 @@ def count(self, numeric_only: bool = False): template_see_also, create_section_header("Examples"), dedent( - """ + """\ >>> ser = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']) >>> ser.expanding().apply(lambda s: s.max() - 2 * s.min()) a -1.0 @@ -222,7 +222,7 @@ def count(self, numeric_only: bool = False): d 2.0 dtype: float64 """ - ).replace("\n", "", 1), + ), window_method="expanding", aggregation_description="custom aggregation function", agg_method="apply", @@ -258,7 +258,7 @@ def apply( numba_notes, create_section_header("Examples"), dedent( - """ + """\ >>> ser = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']) >>> ser.expanding().sum() a 1.0 @@ -267,7 +267,7 @@ def apply( d 10.0 dtype: float64 """ - ).replace("\n", "", 1), + ), window_method="expanding", aggregation_description="sum", agg_method="sum", @@ -297,7 +297,7 @@ def sum( numba_notes, create_section_header("Examples"), dedent( - """ + """\ >>> ser = pd.Series([3, 2, 1, 4], index=['a', 'b', 'c', 'd']) >>> ser.expanding().max() a 3.0 @@ -306,7 +306,7 @@ def sum( d 4.0 dtype: float64 """ - ).replace("\n", "", 1), + ), window_method="expanding", aggregation_description="maximum", agg_method="max", @@ -375,7 +375,7 @@ def min( numba_notes, create_section_header("Examples"), dedent( - """ + """\ >>> ser = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']) >>> ser.expanding().mean() a 1.0 @@ -384,7 +384,7 @@ def min( d 2.5 dtype: float64 """ - ).replace("\n", "", 1), + ), window_method="expanding", aggregation_description="mean", agg_method="mean", @@ -414,7 +414,7 @@ def mean( numba_notes, create_section_header("Examples"), dedent( - """ + """\ >>> ser = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']) >>> ser.expanding().median() a 1.0 @@ -423,7 +423,7 @@ def mean( d 2.5 dtype: float64 """ - ).replace("\n", "", 1), + ), window_method="expanding", aggregation_description="median", agg_method="median", @@ -610,7 +610,7 @@ def sem(self, ddof: int = 1, numeric_only: bool = False): "A minimum of three periods is required for the rolling calculation.\n\n", create_section_header("Examples"), dedent( - """ + """\ >>> ser = pd.Series([-1, 0, 2, -1, 2], index=['a', 'b', 'c', 'd', 'e']) >>> ser.expanding().skew() a NaN @@ -620,7 +620,7 @@ def sem(self, ddof: int = 1, numeric_only: bool = False): e 0.315356 dtype: float64 """ - ).replace("\n", "", 1), + ), window_method="expanding", aggregation_description="unbiased skewness", agg_method="skew", @@ -697,7 +697,7 @@ def kurt(self, numeric_only: bool = False): template_see_also, create_section_header("Examples"), dedent( - """ + """\ >>> ser = pd.Series([1, 2, 3, 4, 5, 6], index=['a', 'b', 'c', 'd', 'e', 'f']) >>> ser.expanding(min_periods=4).quantile(.25) a NaN @@ -708,7 +708,7 @@ def kurt(self, numeric_only: bool = False): f 2.25 dtype: float64 """ - ).replace("\n", "", 1), + ), window_method="expanding", aggregation_description="quantile", agg_method="quantile", @@ -828,7 +828,7 @@ def rank( template_see_also, create_section_header("Examples"), dedent( - """ + """\ >>> ser1 = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']) >>> ser2 = pd.Series([10, 11, 13, 16], index=['a', 'b', 'c', 'd']) >>> ser1.expanding().cov(ser2) @@ -838,7 +838,7 @@ def rank( d 3.333333 dtype: float64 """ - ).replace("\n", "", 1), + ), window_method="expanding", aggregation_description="sample covariance", agg_method="cov", @@ -911,7 +911,7 @@ def cov( ), create_section_header("Examples"), dedent( - """ + """\ >>> ser1 = pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd']) >>> ser2 = pd.Series([10, 11, 13, 16], index=['a', 'b', 'c', 'd']) >>> ser1.expanding().corr(ser2) @@ -921,7 +921,7 @@ def cov( d 0.975900 dtype: float64 """ - ).replace("\n", "", 1), + ), window_method="expanding", aggregation_description="correlation", agg_method="corr",