-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you remove the |
||
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 | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import pandas as pd | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be kept
There was a problem hiding this comment.
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
overpandas.core.window.ExponentialMovingWindowGroupby
?There was a problem hiding this comment.
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