From 337254feb7f0c9d8b8b0eda70990f616a80fe626 Mon Sep 17 00:00:00 2001 From: Shaurya Bisht <87357655+ShauryaDusht@users.noreply.github.com> Date: Mon, 14 Apr 2025 06:18:50 +0000 Subject: [PATCH 01/10] DOC: Add documentation for groupby.ewm() --- doc/source/reference/groupby.rst | 2 + pandas/core/groupby/groupby.py | 138 ++++++++++++++++++++++++++++++- 2 files changed, 138 insertions(+), 2 deletions(-) diff --git a/doc/source/reference/groupby.rst b/doc/source/reference/groupby.rst index fc180c8161a7e..ce9aeeb358c19 100644 --- a/doc/source/reference/groupby.rst +++ b/doc/source/reference/groupby.rst @@ -79,6 +79,7 @@ Function application DataFrameGroupBy.cumsum DataFrameGroupBy.describe DataFrameGroupBy.diff + DataFrameGroupBy.ewm DataFrameGroupBy.ffill DataFrameGroupBy.first DataFrameGroupBy.head @@ -130,6 +131,7 @@ Function application SeriesGroupBy.cumsum SeriesGroupBy.describe SeriesGroupBy.diff + SeriesGroupBy.ewm SeriesGroupBy.ffill SeriesGroupBy.first SeriesGroupBy.head diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 9cfeb53821fbc..888595867d98f 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -3826,14 +3826,148 @@ def expanding(self, *args, **kwargs) -> ExpandingGroupby: @final @Substitution(name="groupby") @Appender(_common_see_also) - def ewm(self, *args, **kwargs) -> ExponentialMovingWindowGroupby: + def ewm( + self, + com: float | None = None, + span: float | None = None, + halflife: float | None = None, + alpha: float | None = None, + min_periods: int = 0, + adjust: bool = True, + ignore_na: bool = False, + axis: Axis = 0, + ) -> ExponentialMovingWindowGroupby: """ - Return an ewm grouper, providing ewm functionality per group. + Return an exponential weighted moving average grouper, providing ewm functionality per group. + + Parameters + ---------- + com : float, optional + Specify decay in terms of center of mass: + :math:`\\alpha = 1 / (1 + com)` for :math:`com \\geq 0`. + + One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must be provided. + + span : float, optional + Specify decay in terms of span: + :math:`\\alpha = 2 / (span + 1)` for :math:`span \\geq 1`. + + One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must be provided. + + halflife : float, optional + Specify decay in terms of half-life: + :math:`\\alpha = 1 - \\exp(-\\ln(2) / halflife)` for :math:`halflife > 0`. + + One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must be provided. + + alpha : float, optional + Specify the smoothing factor :math:`\\alpha` directly, where :math:`0 < \\alpha \\leq 1`. + + One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must be provided. + + min_periods : int, default 0 + Minimum number of observations in window required to have a value; + otherwise, result is ``np.nan``. + + adjust : bool, default True + Divide by decaying adjustment factor in beginning periods to account + for imbalance in relative weightings (viewing EWMA as a moving average). + + If ``False``, the exponentially weighted function is: + + .. math:: + y_t = (1 - \\alpha) y_{t-1} + \\alpha x_t + + If ``True``, the exponentially weighted function is: + + .. math:: + y_t = \\frac{\\sum_{i=0}^t w_i x_{t-i}}{\\sum_{i=0}^t w_i} + + where :math:`w_i = (1 - \\alpha)^i`. + + ignore_na : bool, default False + If ``True``, missing values are ignored in the calculation. + + If ``False``, missing values are treated as missing. + + axis : {0 or 'index', 1 or 'columns'}, default 0 + The axis to use. The value 0 identifies the rows, and 1 identifies the columns. Returns ------- pandas.api.typing.ExponentialMovingWindowGroupby + Return a new grouper with exponential moving window capabilities. + + See Also + -------- + Series.ewm : Exponential weighted moving window functions for Series. + DataFrame.ewm : Exponential weighted moving window functions for DataFrames. + Series.groupby : Apply a function groupby to a Series. + DataFrame.groupby : Apply a function groupby to a DataFrame. + + Notes + ----- + Each group is treated independently, and the exponential weighted calculations + are applied separately to each group. + + The exponential weighted calculation is based on the formula: + + .. math:: + y_t = (1 - \\alpha) y_{t-1} + \\alpha x_t + + where :math:`\\alpha` is the smoothing factor derived from one of the input + decay parameters (``com``, ``span``, ``halflife``, or ``alpha``). + + Only one of ``com``, ``span``, ``halflife``, or ``alpha`` can be specified. + + Examples + -------- + >>> df = pd.DataFrame( + ... { + ... "Class": ["A", "A", "A", "B", "B", "B"], + ... "Value": [10, 20, 30, 40, 50, 60], + ... } + ... ) + >>> df + Class Value + 0 A 10 + 1 A 20 + 2 A 30 + 3 B 40 + 4 B 50 + 5 B 60 + + >>> df.groupby("Class").ewm(span=2).mean() + Value + Class + A 0 10.000000 + 1 17.500000 + 2 26.153846 + B 3 40.000000 + 4 47.500000 + 5 56.153846 + + >>> df.groupby("Class").ewm(alpha=0.5, adjust=False).mean() + Value + Class + A 0 10.000000 + 1 15.000000 + 2 22.500000 + B 3 40.000000 + 4 45.000000 + 5 52.500000 + + >>> df.groupby("Class").ewm(com=1.0, min_periods=1).std() + Value + Class + A 0 0.000000 + 1 7.500000 + 2 10.606602 + B 3 0.000000 + 4 7.500000 + 5 10.606602 """ + from pandas.core.window import ExponentialMovingWindowGroupby return ExponentialMovingWindowGroupby( From bbf5e2ba5c798ac91bd959a52202985d9d578aaf Mon Sep 17 00:00:00 2001 From: Shaurya Bisht <87357655+ShauryaDusht@users.noreply.github.com> Date: Mon, 14 Apr 2025 06:35:55 +0000 Subject: [PATCH 02/10] DOC: Add documentation for groupby.ewm() --- pandas/core/groupby/groupby.py | 36 +++++++++++++++++----------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 888595867d98f..61181fd1fed55 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -3929,7 +3929,7 @@ def ewm( ... } ... ) >>> df - Class Value + Class Value 0 A 10 1 A 20 2 A 30 @@ -3938,8 +3938,8 @@ def ewm( 5 B 60 >>> df.groupby("Class").ewm(span=2).mean() - Value - Class + Value + Class A 0 10.000000 1 17.500000 2 26.153846 @@ -3948,24 +3948,24 @@ def ewm( 5 56.153846 >>> df.groupby("Class").ewm(alpha=0.5, adjust=False).mean() - Value - Class - A 0 10.000000 - 1 15.000000 - 2 22.500000 - B 3 40.000000 - 4 45.000000 - 5 52.500000 + Value + Class + A 0 10.0 + 1 15.0 + 2 22.5 + B 3 40.0 + 4 45.0 + 5 52.5 >>> df.groupby("Class").ewm(com=1.0, min_periods=1).std() Value - Class - A 0 0.000000 - 1 7.500000 - 2 10.606602 - B 3 0.000000 - 4 7.500000 - 5 10.606602 + Class + A 0 NaN + 1 7.071068 + 2 9.636241 + B 3 NaN + 4 7.071068 + 5 9.636241 """ from pandas.core.window import ExponentialMovingWindowGroupby From f4635460b49be326a5126b82663d59c58d4260b4 Mon Sep 17 00:00:00 2001 From: Shaurya Bisht <87357655+ShauryaDusht@users.noreply.github.com> Date: Mon, 14 Apr 2025 06:59:11 +0000 Subject: [PATCH 03/10] DOC: Added docs for groupby.ewm() #61283 --- pandas/core/groupby/groupby.py | 58 ++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 61181fd1fed55..d0de8b720bb6f 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -3835,35 +3835,38 @@ def ewm( min_periods: int = 0, adjust: bool = True, ignore_na: bool = False, - axis: Axis = 0, + axis: int = 0, ) -> ExponentialMovingWindowGroupby: """ - Return an exponential weighted moving average grouper, providing ewm functionality per group. + Return an exponential weighted moving average grouper, + providing ewm functionality per group. Parameters ---------- com : float, optional Specify decay in terms of center of mass: :math:`\\alpha = 1 / (1 + com)` for :math:`com \\geq 0`. - - One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must be provided. + One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must + be provided. span : float, optional Specify decay in terms of span: :math:`\\alpha = 2 / (span + 1)` for :math:`span \\geq 1`. - - One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must be provided. + One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must + be provided. halflife : float, optional Specify decay in terms of half-life: :math:`\\alpha = 1 - \\exp(-\\ln(2) / halflife)` for :math:`halflife > 0`. - - One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must be provided. + One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must + be provided. alpha : float, optional - Specify the smoothing factor :math:`\\alpha` directly, where :math:`0 < \\alpha \\leq 1`. - - One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must be provided. + Specify the smoothing factor + :math:`\\alpha` directly, where :math:`0 < \\alpha \\leq 1`. + + One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must + be provided. min_periods : int, default 0 Minimum number of observations in window required to have a value; @@ -3872,26 +3875,27 @@ def ewm( adjust : bool, default True Divide by decaying adjustment factor in beginning periods to account for imbalance in relative weightings (viewing EWMA as a moving average). - + If ``False``, the exponentially weighted function is: - + .. math:: y_t = (1 - \\alpha) y_{t-1} + \\alpha x_t - + If ``True``, the exponentially weighted function is: - + .. math:: y_t = \\frac{\\sum_{i=0}^t w_i x_{t-i}}{\\sum_{i=0}^t w_i} - + where :math:`w_i = (1 - \\alpha)^i`. ignore_na : bool, default False If ``True``, missing values are ignored in the calculation. - + If ``False``, missing values are treated as missing. axis : {0 or 'index', 1 or 'columns'}, default 0 - The axis to use. The value 0 identifies the rows, and 1 identifies the columns. + The axis to use. The value 0 identifies the rows, and 1 identifies the + columns. Returns ------- @@ -3917,7 +3921,7 @@ def ewm( where :math:`\\alpha` is the smoothing factor derived from one of the input decay parameters (``com``, ``span``, ``halflife``, or ``alpha``). - + Only one of ``com``, ``span``, ``halflife``, or ``alpha`` can be specified. Examples @@ -3939,7 +3943,7 @@ def ewm( >>> df.groupby("Class").ewm(span=2).mean() Value - Class + Class A 0 10.000000 1 17.500000 2 26.153846 @@ -3949,7 +3953,7 @@ def ewm( >>> df.groupby("Class").ewm(alpha=0.5, adjust=False).mean() Value - Class + Class A 0 10.0 1 15.0 2 22.5 @@ -3959,7 +3963,7 @@ def ewm( >>> df.groupby("Class").ewm(com=1.0, min_periods=1).std() Value - Class + Class A 0 NaN 1 7.071068 2 9.636241 @@ -3972,9 +3976,15 @@ def ewm( return ExponentialMovingWindowGroupby( self._selected_obj, - *args, + com=com, + span=span, + halflife=halflife, + alpha=alpha, + min_periods=min_periods, + adjust=adjust, + ignore_na=ignore_na, + axis=axis, _grouper=self._grouper, - **kwargs, ) @final From c7fc3a0ffc224b8c3df8bb0fff43895d7a8274ca Mon Sep 17 00:00:00 2001 From: Shaurya Bisht <87357655+ShauryaDusht@users.noreply.github.com> Date: Mon, 14 Apr 2025 07:53:47 +0000 Subject: [PATCH 04/10] reverted function definition --- pandas/core/groupby/groupby.py | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index d0de8b720bb6f..4653b0b5952ba 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -3826,17 +3826,7 @@ def expanding(self, *args, **kwargs) -> ExpandingGroupby: @final @Substitution(name="groupby") @Appender(_common_see_also) - def ewm( - self, - com: float | None = None, - span: float | None = None, - halflife: float | None = None, - alpha: float | None = None, - min_periods: int = 0, - adjust: bool = True, - ignore_na: bool = False, - axis: int = 0, - ) -> ExponentialMovingWindowGroupby: + def ewm(self, *args, **kwargs) -> ExponentialMovingWindowGroupby: """ Return an exponential weighted moving average grouper, providing ewm functionality per group. @@ -3976,15 +3966,9 @@ def ewm( return ExponentialMovingWindowGroupby( self._selected_obj, - com=com, - span=span, - halflife=halflife, - alpha=alpha, - min_periods=min_periods, - adjust=adjust, - ignore_na=ignore_na, - axis=axis, + *args, _grouper=self._grouper, + **kwargs, ) @final From 21cbbc0f4d102eff0555b67c856cb4a588ede233 Mon Sep 17 00:00:00 2001 From: Shaurya Bisht <87357655+ShauryaDusht@users.noreply.github.com> Date: Mon, 14 Apr 2025 13:10:51 +0000 Subject: [PATCH 05/10] remove See Also section --- pandas/core/groupby/groupby.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 4653b0b5952ba..2be6d5186a40c 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -3892,13 +3892,6 @@ def ewm(self, *args, **kwargs) -> ExponentialMovingWindowGroupby: pandas.api.typing.ExponentialMovingWindowGroupby Return a new grouper with exponential moving window capabilities. - See Also - -------- - Series.ewm : Exponential weighted moving window functions for Series. - DataFrame.ewm : Exponential weighted moving window functions for DataFrames. - Series.groupby : Apply a function groupby to a Series. - DataFrame.groupby : Apply a function groupby to a DataFrame. - Notes ----- Each group is treated independently, and the exponential weighted calculations From 53ac271f3a5bf379d950c36ef9d9836cbbaf5251 Mon Sep 17 00:00:00 2001 From: Shaurya Bisht <87357655+ShauryaDusht@users.noreply.github.com> Date: Mon, 14 Apr 2025 13:43:09 +0000 Subject: [PATCH 06/10] improvements --- pandas/core/groupby/groupby.py | 48 +++++++++------------------------- 1 file changed, 12 insertions(+), 36 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 2be6d5186a40c..cc9a694efd365 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -3828,8 +3828,7 @@ def expanding(self, *args, **kwargs) -> ExpandingGroupby: @Appender(_common_see_also) def ewm(self, *args, **kwargs) -> ExponentialMovingWindowGroupby: """ - Return an exponential weighted moving average grouper, - providing ewm functionality per group. + Provide exponential weighted functions for the groupby. Parameters ---------- @@ -3838,54 +3837,34 @@ def ewm(self, *args, **kwargs) -> ExponentialMovingWindowGroupby: :math:`\\alpha = 1 / (1 + com)` for :math:`com \\geq 0`. One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must be provided. - span : float, optional Specify decay in terms of span: :math:`\\alpha = 2 / (span + 1)` for :math:`span \\geq 1`. One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must be provided. - halflife : float, optional Specify decay in terms of half-life: :math:`\\alpha = 1 - \\exp(-\\ln(2) / halflife)` for :math:`halflife > 0`. One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must be provided. - alpha : float, optional - Specify the smoothing factor - :math:`\\alpha` directly, where :math:`0 < \\alpha \\leq 1`. - + Specify the smoothing factor :math:`\\alpha` directly, where :math:`0 < \\alpha \\leq 1`. One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must be provided. - min_periods : int, default 0 Minimum number of observations in window required to have a value; otherwise, result is ``np.nan``. - adjust : bool, default True Divide by decaying adjustment factor in beginning periods to account for imbalance in relative weightings (viewing EWMA as a moving average). - - If ``False``, the exponentially weighted function is: - - .. math:: - y_t = (1 - \\alpha) y_{t-1} + \\alpha x_t - - If ``True``, the exponentially weighted function is: - - .. math:: - y_t = \\frac{\\sum_{i=0}^t w_i x_{t-i}}{\\sum_{i=0}^t w_i} - - where :math:`w_i = (1 - \\alpha)^i`. - ignore_na : bool, default False If ``True``, missing values are ignored in the calculation. - If ``False``, missing values are treated as missing. - axis : {0 or 'index', 1 or 'columns'}, default 0 The axis to use. The value 0 identifies the rows, and 1 identifies the columns. + *args, **kwargs + Additional arguments and keyword arguments passed to the function. Returns ------- @@ -3897,15 +3876,12 @@ def ewm(self, *args, **kwargs) -> ExponentialMovingWindowGroupby: Each group is treated independently, and the exponential weighted calculations are applied separately to each group. - The exponential weighted calculation is based on the formula: - - .. math:: - y_t = (1 - \\alpha) y_{t-1} + \\alpha x_t + When ``adjust=True``, weighted averages are calculated using weights + :math:`w_i = (1-\\alpha)^i` where :math:`i` is the number of periods from the + observations being weighted to the current period. - where :math:`\\alpha` is the smoothing factor derived from one of the input - decay parameters (``com``, ``span``, ``halflife``, or ``alpha``). - - Only one of ``com``, ``span``, ``halflife``, or ``alpha`` can be specified. + When ``adjust=False``, the calculation follows the recursive formula: + :math:`y_t = (1 - \\alpha) y_{t-1} + \\alpha x_t`. Examples -------- @@ -3916,7 +3892,7 @@ def ewm(self, *args, **kwargs) -> ExponentialMovingWindowGroupby: ... } ... ) >>> df - Class Value + Class Value 0 A 10 1 A 20 2 A 30 @@ -3925,7 +3901,7 @@ def ewm(self, *args, **kwargs) -> ExponentialMovingWindowGroupby: 5 B 60 >>> df.groupby("Class").ewm(span=2).mean() - Value + Value Class A 0 10.000000 1 17.500000 @@ -3935,7 +3911,7 @@ def ewm(self, *args, **kwargs) -> ExponentialMovingWindowGroupby: 5 56.153846 >>> df.groupby("Class").ewm(alpha=0.5, adjust=False).mean() - Value + Value Class A 0 10.0 1 15.0 From cd2c7a5174fd8f750bfd179f6a468243cd28fdef Mon Sep 17 00:00:00 2001 From: Shaurya Bisht <87357655+ShauryaDusht@users.noreply.github.com> Date: Mon, 14 Apr 2025 13:49:07 +0000 Subject: [PATCH 07/10] ruff checks updated --- pandas/core/groupby/groupby.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index cc9a694efd365..a1e3632fd08f8 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -3848,7 +3848,8 @@ def ewm(self, *args, **kwargs) -> ExponentialMovingWindowGroupby: One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must be provided. alpha : float, optional - Specify the smoothing factor :math:`\\alpha` directly, where :math:`0 < \\alpha \\leq 1`. + Specify the smoothing factor :math:`\\alpha` directly, + where :math:`0 < \\alpha \\leq 1`. One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must be provided. min_periods : int, default 0 @@ -3875,11 +3876,9 @@ def ewm(self, *args, **kwargs) -> ExponentialMovingWindowGroupby: ----- Each group is treated independently, and the exponential weighted calculations are applied separately to each group. - When ``adjust=True``, weighted averages are calculated using weights :math:`w_i = (1-\\alpha)^i` where :math:`i` is the number of periods from the observations being weighted to the current period. - When ``adjust=False``, the calculation follows the recursive formula: :math:`y_t = (1 - \\alpha) y_{t-1} + \\alpha x_t`. From 4b1b051d04c2f421456de8550ef05a88e17fa585 Mon Sep 17 00:00:00 2001 From: Shaurya Bisht <87357655+ShauryaDusht@users.noreply.github.com> Date: Mon, 14 Apr 2025 13:56:36 +0000 Subject: [PATCH 08/10] ruff checks --- pandas/core/groupby/groupby.py | 6 +++--- pandas/tests/frame/test_query_eval.py | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index a1e3632fd08f8..4c01444f80f9a 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -3848,7 +3848,7 @@ def ewm(self, *args, **kwargs) -> ExponentialMovingWindowGroupby: One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must be provided. alpha : float, optional - Specify the smoothing factor :math:`\\alpha` directly, + Specify the smoothing factor :math:`\\alpha` directly, where :math:`0 < \\alpha \\leq 1`. One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must be provided. @@ -3876,8 +3876,8 @@ def ewm(self, *args, **kwargs) -> ExponentialMovingWindowGroupby: ----- Each group is treated independently, and the exponential weighted calculations are applied separately to each group. - When ``adjust=True``, weighted averages are calculated using weights - :math:`w_i = (1-\\alpha)^i` where :math:`i` is the number of periods from the + When ``adjust=True``, weighted averages are calculated using weights + :math:`w_i = (1-\\alpha)^i` where :math:`i` is the number of periods from the observations being weighted to the current period. When ``adjust=False``, the calculation follows the recursive formula: :math:`y_t = (1 - \\alpha) y_{t-1} + \\alpha x_t`. diff --git a/pandas/tests/frame/test_query_eval.py b/pandas/tests/frame/test_query_eval.py index f93105498ac79..b599be5d042fe 100644 --- a/pandas/tests/frame/test_query_eval.py +++ b/pandas/tests/frame/test_query_eval.py @@ -168,7 +168,7 @@ def test_query_duplicate_column_name(self, engine, parser): } ).rename(columns={"B": "A"}) - res = df.query('C == 1', engine=engine, parser=parser) + res = df.query("C == 1", engine=engine, parser=parser) expect = DataFrame( [[1, 1, 1]], @@ -1411,7 +1411,7 @@ def test_expr_with_column_name_with_backtick_and_hash(self): def test_expr_with_column_name_with_backtick(self): # GH 59285 df = DataFrame({"a`b": (1, 2, 3), "ab": (4, 5, 6)}) - result = df.query("`a``b` < 2") # noqa + result = df.query("`a``b` < 2") # Note: Formatting checks may wrongly consider the above ``inline code``. expected = df[df["a`b"] < 2] tm.assert_frame_equal(result, expected) From ef2d9dd0dfa3c1f93541d737b126482402fc1b3a Mon Sep 17 00:00:00 2001 From: Shaurya Bisht <87357655+ShauryaDusht@users.noreply.github.com> Date: Mon, 14 Apr 2025 14:27:37 +0000 Subject: [PATCH 09/10] docstring checks --- pandas/core/groupby/groupby.py | 78 ++++++++++------------------------ 1 file changed, 22 insertions(+), 56 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 4c01444f80f9a..95c3c895e895c 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -152,7 +152,6 @@ class providing the base-class of operations. from pandas.core.indexers.objects import BaseIndexer from pandas.core.resample import Resampler from pandas.core.window import ( - ExpandingGroupby, ExponentialMovingWindowGroupby, RollingGroupby, ) @@ -3802,27 +3801,6 @@ def rolling( _as_index=self.as_index, ) - @final - @Substitution(name="groupby") - @Appender(_common_see_also) - def expanding(self, *args, **kwargs) -> ExpandingGroupby: - """ - Return an expanding grouper, providing expanding - functionality per group. - - Returns - ------- - pandas.api.typing.ExpandingGroupby - """ - from pandas.core.window import ExpandingGroupby - - return ExpandingGroupby( - self._selected_obj, - *args, - _grouper=self._grouper, - **kwargs, - ) - @final @Substitution(name="groupby") @Appender(_common_see_also) @@ -3832,53 +3810,41 @@ def ewm(self, *args, **kwargs) -> ExponentialMovingWindowGroupby: Parameters ---------- - com : float, optional - Specify decay in terms of center of mass: - :math:`\\alpha = 1 / (1 + com)` for :math:`com \\geq 0`. - One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must - be provided. - span : float, optional - Specify decay in terms of span: - :math:`\\alpha = 2 / (span + 1)` for :math:`span \\geq 1`. - One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must - be provided. - halflife : float, optional - Specify decay in terms of half-life: - :math:`\\alpha = 1 - \\exp(-\\ln(2) / halflife)` for :math:`halflife > 0`. - One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must - be provided. - alpha : float, optional - Specify the smoothing factor :math:`\\alpha` directly, - where :math:`0 < \\alpha \\leq 1`. - One and only one of ``com``, ``span``, ``halflife``, or ``alpha`` must - be provided. - min_periods : int, default 0 - Minimum number of observations in window required to have a value; - otherwise, result is ``np.nan``. - adjust : bool, default True - Divide by decaying adjustment factor in beginning periods to account - for imbalance in relative weightings (viewing EWMA as a moving average). - ignore_na : bool, default False - If ``True``, missing values are ignored in the calculation. - If ``False``, missing values are treated as missing. - axis : {0 or 'index', 1 or 'columns'}, default 0 - The axis to use. The value 0 identifies the rows, and 1 identifies the - columns. - *args, **kwargs - Additional arguments and keyword arguments passed to the function. + *args + Arguments to be passed to + :meth:`~pandas.core.window.ExponentialMovingWindow`. + **kwargs + Keyword arguments to be passed to + :meth:`~pandas.core.window.ExponentialMovingWindow`. + These can include: + - com : float, optional + - span : float, optional + - halflife : float, optional + - alpha : float, optional + - min_periods : int, default 0 + - adjust : bool, default True + - ignore_na : bool, default False + - axis : {0 or 'index', 1 or 'columns'}, default 0 Returns ------- pandas.api.typing.ExponentialMovingWindowGroupby Return a new grouper with exponential moving window capabilities. + See Also + -------- + pandas.DataFrame.ewm : Exponential weighted function for DataFrame. + pandas.Series.ewm : Exponential weighted function for Series. + Notes ----- Each group is treated independently, and the exponential weighted calculations are applied separately to each group. + When ``adjust=True``, weighted averages are calculated using weights :math:`w_i = (1-\\alpha)^i` where :math:`i` is the number of periods from the observations being weighted to the current period. + When ``adjust=False``, the calculation follows the recursive formula: :math:`y_t = (1 - \\alpha) y_{t-1} + \\alpha x_t`. From a0cf60a1727afe8ac25fd653c58d71802805fccb Mon Sep 17 00:00:00 2001 From: Shaurya Bisht <87357655+ShauryaDusht@users.noreply.github.com> Date: Mon, 14 Apr 2025 14:40:59 +0000 Subject: [PATCH 10/10] update docstring --- pandas/core/groupby/groupby.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 95c3c895e895c..12fc6896bdb8f 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -3831,11 +3831,6 @@ def ewm(self, *args, **kwargs) -> ExponentialMovingWindowGroupby: pandas.api.typing.ExponentialMovingWindowGroupby Return a new grouper with exponential moving window capabilities. - See Also - -------- - pandas.DataFrame.ewm : Exponential weighted function for DataFrame. - pandas.Series.ewm : Exponential weighted function for Series. - Notes ----- Each group is treated independently, and the exponential weighted calculations