Skip to content

DOC: Add documentation for groupby.ewm() #61283

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 5 commits into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions doc/source/reference/groupby.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Function application
DataFrameGroupBy.cumsum
DataFrameGroupBy.describe
DataFrameGroupBy.diff
DataFrameGroupBy.ewm
DataFrameGroupBy.ffill
DataFrameGroupBy.first
DataFrameGroupBy.head
Expand Down Expand Up @@ -130,6 +131,7 @@ Function application
SeriesGroupBy.cumsum
SeriesGroupBy.describe
SeriesGroupBy.diff
SeriesGroupBy.ewm
SeriesGroupBy.ffill
SeriesGroupBy.first
SeriesGroupBy.head
Expand Down
66 changes: 63 additions & 3 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -3824,15 +3824,75 @@ def expanding(self, *args, **kwargs) -> ExpandingGroupby:
)

@final
@Substitution(name="groupby")
@Appender(_common_see_also)
def ewm(self, *args, **kwargs) -> ExponentialMovingWindowGroupby:
"""
Return an ewm grouper, providing ewm functionality per group.

Parameters
----------
*args : tuple
Positional arguments passed to the EWM window constructor.
**kwargs : dict
Keyword arguments passed to the EWM window constructor, such as:

com : float, optional
Specify decay in terms of center of mass.
``span``, ``halflife``, and ``alpha`` are alternative ways to specify decay.
span : float, optional
Specify decay in terms of span.
halflife : float, optional
Specify decay in terms of half-life.
alpha : float, optional
Specify smoothing factor directly.
min_periods : int, default 0
Minimum number of observations in the window required to have a value;
otherwise, result is ``np.nan``.
adjust : bool, default True
Divide by decaying adjustment factor to account for imbalance in relative weights.
ignore_na : bool, default False
Ignore missing values when calculating weights.
times : str or array-like of datetime64, optional
Times corresponding to the observations.
axis : {0 or 'index', 1 or 'columns'}, default 0
Axis along which the EWM function is applied.

Returns
-------
pandas.api.typing.ExponentialMovingWindowGroupby
Copy link
Member

Choose a reason for hiding this comment

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

This should be kept

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the feedback! Just to clarify, is there a particular reason to prefer pandas.api.typing.ExponentialMovingWindowGroupby over pandas.core.window.ExponentialMovingWindowGroupby?

Copy link
Member

Choose a reason for hiding this comment

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

pandas.core is not a public module

pandas.core.window.ExponentialMovingWindowGroupby
An object that supports exponentially weighted moving transformations over each group.

See Also
--------
Series.ewm : EWM transformations for Series.
DataFrame.ewm : EWM transformations for DataFrames.
Series.groupby : Apply a function groupby to a Series.
DataFrame.groupby : Apply a function groupby.

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(com=0.5).mean().reset_index(drop=True)
Copy link
Member

Choose a reason for hiding this comment

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

Can you remove the reset_index call?

Value
0 10.000000
1 17.500000
2 26.153846
3 40.000000
4 47.500000
5 56.153846
"""
from pandas.core.window import ExponentialMovingWindowGroupby

Expand Down
19 changes: 19 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pandas as pd
Copy link
Member

Choose a reason for hiding this comment

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

This should be removed


pd.set_option('display.float_format', '{:.2f}'.format)

df = pd.DataFrame([[15.22345676543234567]*6], columns=[1,2,3,4,5,6])

# Default float_format works:
print(df) # ✅ Columns display with 2 decimals

default_fmt = '{:.2f}'
special_fmt = {1: '{:.1f}'}

formats = {
col: special_fmt.get(col, default_fmt)
for col in df.columns
}

styled = df.style.format(formats)
styled.to_html("styled.html")
Loading