Skip to content

Commit 4ec05b0

Browse files
committed
updated doc and references
1 parent adec21f commit 4ec05b0

File tree

3 files changed

+85
-4
lines changed

3 files changed

+85
-4
lines changed

doc/source/reference/groupby.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Function application
7979
DataFrameGroupBy.cumsum
8080
DataFrameGroupBy.describe
8181
DataFrameGroupBy.diff
82+
DataFrameGroupBy.ewm
8283
DataFrameGroupBy.ffill
8384
DataFrameGroupBy.first
8485
DataFrameGroupBy.head
@@ -130,6 +131,7 @@ Function application
130131
SeriesGroupBy.cumsum
131132
SeriesGroupBy.describe
132133
SeriesGroupBy.diff
134+
SeriesGroupBy.ewm
133135
SeriesGroupBy.ffill
134136
SeriesGroupBy.first
135137
SeriesGroupBy.head

pandas/core/groupby/groupby.py

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3824,15 +3824,75 @@ def expanding(self, *args, **kwargs) -> ExpandingGroupby:
38243824
)
38253825

38263826
@final
3827-
@Substitution(name="groupby")
3828-
@Appender(_common_see_also)
38293827
def ewm(self, *args, **kwargs) -> ExponentialMovingWindowGroupby:
38303828
"""
3831-
Return an ewm grouper, providing ewm functionality per group.
3829+
Return an exponentially weighted moving (EWM) grouper, providing EWM functionality per group.
3830+
3831+
Parameters
3832+
----------
3833+
*args : tuple
3834+
Positional arguments passed to the EWM window constructor.
3835+
**kwargs : dict
3836+
Keyword arguments passed to the EWM window constructor, such as:
3837+
3838+
com : float, optional
3839+
Specify decay in terms of center of mass.
3840+
``span``, ``halflife``, and ``alpha`` are alternative ways to specify decay.
3841+
span : float, optional
3842+
Specify decay in terms of span.
3843+
halflife : float, optional
3844+
Specify decay in terms of half-life.
3845+
alpha : float, optional
3846+
Specify smoothing factor directly.
3847+
min_periods : int, default 0
3848+
Minimum number of observations in the window required to have a value;
3849+
otherwise, result is ``np.nan``.
3850+
adjust : bool, default True
3851+
Divide by decaying adjustment factor to account for imbalance in relative weights.
3852+
ignore_na : bool, default False
3853+
Ignore missing values when calculating weights.
3854+
times : str or array-like of datetime64, optional
3855+
Times corresponding to the observations.
3856+
axis : {0 or 'index', 1 or 'columns'}, default 0
3857+
Axis along which the EWM function is applied.
38323858
38333859
Returns
38343860
-------
3835-
pandas.api.typing.ExponentialMovingWindowGroupby
3861+
pandas.core.window.ExponentialMovingWindowGroupby
3862+
An object that supports exponentially weighted moving transformations over each group.
3863+
3864+
See Also
3865+
--------
3866+
Series.ewm : EWM transformations for Series.
3867+
DataFrame.ewm : EWM transformations for DataFrames.
3868+
Series.groupby : Apply a function groupby to a Series.
3869+
DataFrame.groupby : Apply a function groupby.
3870+
3871+
Examples
3872+
--------
3873+
>>> df = pd.DataFrame(
3874+
... {
3875+
... "Class": ["A", "A", "A", "B", "B", "B"],
3876+
... "Value": [10, 20, 30, 40, 50, 60],
3877+
... }
3878+
... )
3879+
>>> df
3880+
Class Value
3881+
0 A 10
3882+
1 A 20
3883+
2 A 30
3884+
3 B 40
3885+
4 B 50
3886+
5 B 60
3887+
3888+
>>> df.groupby("Class").ewm(com=0.5).mean().reset_index(drop=True)
3889+
Value
3890+
0 10.000000
3891+
1 17.500000
3892+
2 26.153846
3893+
3 40.000000
3894+
4 47.500000
3895+
5 56.153846
38363896
"""
38373897
from pandas.core.window import ExponentialMovingWindowGroupby
38383898

test.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import pandas as pd
2+
3+
pd.set_option('display.float_format', '{:.2f}'.format)
4+
5+
df = pd.DataFrame([[15.22345676543234567]*6], columns=[1,2,3,4,5,6])
6+
7+
# Default float_format works:
8+
print(df) # ✅ Columns display with 2 decimals
9+
10+
default_fmt = '{:.2f}'
11+
special_fmt = {1: '{:.1f}'}
12+
13+
formats = {
14+
col: special_fmt.get(col, default_fmt)
15+
for col in df.columns
16+
}
17+
18+
styled = df.style.format(formats)
19+
styled.to_html("styled.html")

0 commit comments

Comments
 (0)