Skip to content

Commit 5949333

Browse files
DOC: Fixing EX01 - Added examples (#53930)
* Examples Resampler.ohlc, prod, size, sem, std, sum, var * Updated code_checks.sh * Corrected sum docstring * corrected default no, removed print --------- Co-authored-by: Matthew Roeschke <[email protected]>
1 parent ad17842 commit 5949333

File tree

3 files changed

+169
-9
lines changed

3 files changed

+169
-9
lines changed

ci/code_checks.sh

-8
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
118118
pandas.io.stata.StataReader.value_labels \
119119
pandas.io.stata.StataReader.variable_labels \
120120
pandas.io.stata.StataWriter.write_file \
121-
pandas.core.resample.Resampler.ohlc \
122-
pandas.core.resample.Resampler.prod \
123-
pandas.core.resample.Resampler.size \
124-
pandas.core.resample.Resampler.sem \
125-
pandas.core.resample.Resampler.std \
126-
pandas.core.resample.Resampler.sum \
127-
pandas.core.resample.Resampler.var \
128-
pandas.core.resample.Resampler.quantile \
129121
pandas.describe_option \
130122
pandas.reset_option \
131123
pandas.get_option \

pandas/core/groupby/groupby.py

+42
Original file line numberDiff line numberDiff line change
@@ -2795,6 +2795,20 @@ def sem(self, ddof: int = 1, numeric_only: bool = False):
27952795
a
27962796
1 1.5 4.5
27972797
2 0.5 2.0
2798+
2799+
For Resampler:
2800+
2801+
>>> ser = pd.Series([1, 3, 2, 4, 3, 8],
2802+
... index=pd.DatetimeIndex(['2023-01-01',
2803+
... '2023-01-10',
2804+
... '2023-01-15',
2805+
... '2023-02-01',
2806+
... '2023-02-10',
2807+
... '2023-02-15']))
2808+
>>> ser.resample('MS').sem()
2809+
2023-01-01 0.577350
2810+
2023-02-01 1.527525
2811+
Freq: MS, dtype: float64
27982812
"""
27992813
if numeric_only and self.obj.ndim == 1 and not is_numeric_dtype(self.obj.dtype):
28002814
raise TypeError(
@@ -2851,6 +2865,20 @@ def size(self) -> DataFrame | Series:
28512865
1 2
28522866
7 1
28532867
dtype: int64
2868+
2869+
For Resampler:
2870+
2871+
>>> ser = pd.Series([1, 2, 3], index=pd.DatetimeIndex(
2872+
... ['2023-01-01', '2023-01-15', '2023-02-01']))
2873+
>>> ser
2874+
2023-01-01 1
2875+
2023-01-15 2
2876+
2023-02-01 3
2877+
dtype: int64
2878+
>>> ser.resample('MS').size()
2879+
2023-01-01 2
2880+
2023-02-01 1
2881+
Freq: MS, dtype: int64
28542882
"""
28552883
result = self.grouper.size()
28562884

@@ -3303,6 +3331,20 @@ def ohlc(self) -> DataFrame:
33033331
open high low close open high low close
33043332
CAC 2.3 4.5 1.0 1.0 9.0 9.4 1.0 1.0
33053333
SPX 1.2 8.9 1.2 2.0 3.4 8.8 3.4 8.2
3334+
3335+
For Resampler:
3336+
3337+
>>> ser = pd.Series([1, 3, 2, 4, 3, 5],
3338+
... index=pd.DatetimeIndex(['2023-01-01',
3339+
... '2023-01-10',
3340+
... '2023-01-15',
3341+
... '2023-02-01',
3342+
... '2023-02-10',
3343+
... '2023-02-15']))
3344+
>>> ser.resample('MS').ohlc()
3345+
open high low close
3346+
2023-01-01 1 3 1 2
3347+
2023-02-01 4 5 3 5
33063348
"""
33073349
if self.obj.ndim == 1:
33083350
obj = self._selected_obj

pandas/core/resample.py

+127-1
Original file line numberDiff line numberDiff line change
@@ -1109,18 +1109,89 @@ def sum(
11091109
*args,
11101110
**kwargs,
11111111
):
1112+
"""
1113+
Compute sum of group values.
1114+
1115+
Parameters
1116+
----------
1117+
numeric_only : bool, default False
1118+
Include only float, int, boolean columns.
1119+
1120+
.. versionchanged:: 2.0.0
1121+
1122+
numeric_only no longer accepts ``None``.
1123+
1124+
min_count : int, default 0
1125+
The required number of valid values to perform the operation. If fewer
1126+
than ``min_count`` non-NA values are present the result will be NA.
1127+
1128+
Returns
1129+
-------
1130+
Series or DataFrame
1131+
Computed sum of values within each group.
1132+
1133+
Examples
1134+
--------
1135+
>>> ser = pd.Series([1, 2, 3, 4], index=pd.DatetimeIndex(
1136+
... ['2023-01-01', '2023-01-15', '2023-02-01', '2023-02-15']))
1137+
>>> ser
1138+
2023-01-01 1
1139+
2023-01-15 2
1140+
2023-02-01 3
1141+
2023-02-15 4
1142+
dtype: int64
1143+
>>> ser.resample('MS').sum()
1144+
2023-01-01 3
1145+
2023-02-01 7
1146+
Freq: MS, dtype: int64
1147+
"""
11121148
maybe_warn_args_and_kwargs(type(self), "sum", args, kwargs)
11131149
nv.validate_resampler_func("sum", args, kwargs)
11141150
return self._downsample("sum", numeric_only=numeric_only, min_count=min_count)
11151151

1116-
@doc(GroupBy.prod)
11171152
def prod(
11181153
self,
11191154
numeric_only: bool = False,
11201155
min_count: int = 0,
11211156
*args,
11221157
**kwargs,
11231158
):
1159+
"""
1160+
Compute prod of group values.
1161+
1162+
Parameters
1163+
----------
1164+
numeric_only : bool, default False
1165+
Include only float, int, boolean columns.
1166+
1167+
.. versionchanged:: 2.0.0
1168+
1169+
numeric_only no longer accepts ``None``.
1170+
1171+
min_count : int, default 0
1172+
The required number of valid values to perform the operation. If fewer
1173+
than ``min_count`` non-NA values are present the result will be NA.
1174+
1175+
Returns
1176+
-------
1177+
Series or DataFrame
1178+
Computed prod of values within each group.
1179+
1180+
Examples
1181+
--------
1182+
>>> ser = pd.Series([1, 2, 3, 4], index=pd.DatetimeIndex(
1183+
... ['2023-01-01', '2023-01-15', '2023-02-01', '2023-02-15']))
1184+
>>> ser
1185+
2023-01-01 1
1186+
2023-01-15 2
1187+
2023-02-01 3
1188+
2023-02-15 4
1189+
dtype: int64
1190+
>>> ser.resample('MS').prod()
1191+
2023-01-01 2
1192+
2023-02-01 12
1193+
Freq: MS, dtype: int64
1194+
"""
11241195
maybe_warn_args_and_kwargs(type(self), "prod", args, kwargs)
11251196
nv.validate_resampler_func("prod", args, kwargs)
11261197
return self._downsample("prod", numeric_only=numeric_only, min_count=min_count)
@@ -1292,6 +1363,21 @@ def std(
12921363
-------
12931364
DataFrame or Series
12941365
Standard deviation of values within each group.
1366+
1367+
Examples
1368+
--------
1369+
1370+
>>> ser = pd.Series([1, 3, 2, 4, 3, 8],
1371+
... index=pd.DatetimeIndex(['2023-01-01',
1372+
... '2023-01-10',
1373+
... '2023-01-15',
1374+
... '2023-02-01',
1375+
... '2023-02-10',
1376+
... '2023-02-15']))
1377+
>>> ser.resample('MS').std()
1378+
2023-01-01 1.000000
1379+
2023-02-01 2.645751
1380+
Freq: MS, dtype: float64
12951381
"""
12961382
maybe_warn_args_and_kwargs(type(self), "std", args, kwargs)
12971383
nv.validate_resampler_func("std", args, kwargs)
@@ -1325,6 +1411,26 @@ def var(
13251411
-------
13261412
DataFrame or Series
13271413
Variance of values within each group.
1414+
1415+
Examples
1416+
--------
1417+
1418+
>>> ser = pd.Series([1, 3, 2, 4, 3, 8],
1419+
... index=pd.DatetimeIndex(['2023-01-01',
1420+
... '2023-01-10',
1421+
... '2023-01-15',
1422+
... '2023-02-01',
1423+
... '2023-02-10',
1424+
... '2023-02-15']))
1425+
>>> ser.resample('MS').var()
1426+
2023-01-01 1.0
1427+
2023-02-01 7.0
1428+
Freq: MS, dtype: float64
1429+
1430+
>>> ser.resample('MS').var(ddof=0)
1431+
2023-01-01 0.666667
1432+
2023-02-01 4.666667
1433+
Freq: MS, dtype: float64
13281434
"""
13291435
maybe_warn_args_and_kwargs(type(self), "var", args, kwargs)
13301436
nv.validate_resampler_func("var", args, kwargs)
@@ -1421,6 +1527,26 @@ def quantile(self, q: float | AnyArrayLike = 0.5, **kwargs):
14211527
DataFrameGroupBy.quantile
14221528
Return a DataFrame, where the columns are groupby columns,
14231529
and the values are its quantiles.
1530+
1531+
Examples
1532+
--------
1533+
1534+
>>> ser = pd.Series([1, 3, 2, 4, 3, 8],
1535+
... index=pd.DatetimeIndex(['2023-01-01',
1536+
... '2023-01-10',
1537+
... '2023-01-15',
1538+
... '2023-02-01',
1539+
... '2023-02-10',
1540+
... '2023-02-15']))
1541+
>>> ser.resample('MS').quantile()
1542+
2023-01-01 2.0
1543+
2023-02-01 4.0
1544+
Freq: MS, dtype: float64
1545+
1546+
>>> ser.resample('MS').quantile(.25)
1547+
2023-01-01 1.5
1548+
2023-02-01 3.5
1549+
Freq: MS, dtype: float64
14241550
"""
14251551
return self._downsample("quantile", q=q, **kwargs)
14261552

0 commit comments

Comments
 (0)