From 9a6794de0f822ebd53ae6c63f079169384dbbcd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dea=20Mar=C3=ADa=20L=C3=A9on?= Date: Wed, 28 Jun 2023 17:33:43 +0200 Subject: [PATCH 1/4] Examples Resampler.asfreq, count, nunique, max, mean, median --- ci/code_checks.sh | 6 ---- pandas/core/groupby/generic.py | 16 +++++++++++ pandas/core/groupby/groupby.py | 29 ++++++++++++++++++++ pandas/core/resample.py | 50 ++++++++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 6 deletions(-) diff --git a/ci/code_checks.sh b/ci/code_checks.sh index f4a0488581606..5b6a065fb7bb5 100755 --- a/ci/code_checks.sh +++ b/ci/code_checks.sh @@ -125,12 +125,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then pandas.core.resample.Resampler.indices \ pandas.core.resample.Resampler.get_group \ pandas.core.resample.Resampler.ffill \ - pandas.core.resample.Resampler.asfreq \ - pandas.core.resample.Resampler.count \ - pandas.core.resample.Resampler.nunique \ - pandas.core.resample.Resampler.max \ - pandas.core.resample.Resampler.mean \ - pandas.core.resample.Resampler.median \ pandas.core.resample.Resampler.min \ pandas.core.resample.Resampler.ohlc \ pandas.core.resample.Resampler.prod \ diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index 6c8e9c3fe9a75..1970abe38f289 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -633,6 +633,7 @@ def nunique(self, dropna: bool = True) -> Series | DataFrame: Examples -------- + For SeriesGroupby: >>> lst = ['a', 'a', 'b', 'b'] >>> ser = pd.Series([1, 2, 3, 3], index=lst) @@ -646,6 +647,21 @@ def nunique(self, dropna: bool = True) -> Series | DataFrame: a 2 b 1 dtype: int64 + + For Resampler: + + >>> ser = pd.Series([1, 2, 3, 3], index=pd.DatetimeIndex( + ... ['2023-01-01', '2023-01-15', '2023-02-01', '2023-02-15'])) + >>> ser + 2023-01-01 1 + 2023-01-15 2 + 2023-02-01 3 + 2023-02-15 3 + dtype: int64 + >>> ser.resample('M').nunique() + 2023-01-31 2 + 2023-02-28 1 + Freq: M, dtype: int64 """ ids, _, _ = self.grouper.group_info diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 1a17fef071a2f..3fe19a5b0c283 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -2104,6 +2104,21 @@ def count(self) -> NDFrameT: a 1 0 2 7 1 1 + + For Resampler: + + >>> ser = pd.Series([1, 2, 3, 4], index=pd.DatetimeIndex( + ... ['2023-01-01', '2023-01-15', '2023-02-01', '2023-02-15'])) + >>> ser + 2023-01-01 1 + 2023-01-15 2 + 2023-02-01 3 + 2023-02-15 4 + dtype: int64 + >>> ser.resample('M').count() + 2023-01-31 2 + 2023-02-28 2 + Freq: M, dtype: int64 """ data = self._get_data_to_aggregate() ids, _, ngroups = self.grouper.group_info @@ -2287,6 +2302,20 @@ def median(self, numeric_only: bool = False): a b dog 3.0 4.0 mouse 7.0 3.0 + + For Resampler: + + >>> ser = pd.Series([1, 2, 3, 3, 4, 5], + ... index=pd.DatetimeIndex(['2023-01-01', + ... '2023-01-10', + ... '2023-01-15', + ... '2023-02-01', + ... '2023-02-10', + ... '2023-02-15'])) + >>> ser.resample('M').median() + 2023-01-31 2.0 + 2023-02-28 4.0 + Freq: M, dtype: float64 """ result = self._cython_agg_general( "median", diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 9566a2f113b36..2b60b9ec4c769 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -1031,6 +1031,22 @@ def asfreq(self, fill_value=None): -------- Series.asfreq: Convert TimeSeries to specified frequency. DataFrame.asfreq: Convert TimeSeries to specified frequency. + + Examples + -------- + + >>> ser = pd.Series([1, 2, 3, 4], index=pd.DatetimeIndex( + ... ['2023-01-01', '2023-01-31', '2023-02-01', '2023-02-28'])) + >>> ser + 2023-01-01 1 + 2023-01-31 2 + 2023-02-01 3 + 2023-02-28 4 + dtype: int64 + >>> ser.resample('M').asfreq() + 2023-01-31 2 + 2023-02-28 4 + Freq: M, dtype: int64 """ return self._upsample("asfreq", fill_value=fill_value) @@ -1075,6 +1091,24 @@ def max( *args, **kwargs, ): + """ + Compute max value of group. + + Examples + -------- + >>> ser = pd.Series([1, 2, 3, 4], index=pd.DatetimeIndex( + ... ['2023-01-01', '2023-01-15', '2023-02-01', '2023-02-15'])) + >>> ser + 2023-01-01 1 + 2023-01-15 2 + 2023-02-01 3 + 2023-02-15 4 + dtype: int64 + >>> ser.resample('M').max() + 2023-01-31 2 + 2023-02-28 4 + Freq: M, dtype: int64 + """ maybe_warn_args_and_kwargs(type(self), "max", args, kwargs) nv.validate_resampler_func("max", args, kwargs) return self._downsample("max", numeric_only=numeric_only, min_count=min_count) @@ -1131,6 +1165,22 @@ def mean( ------- DataFrame or Series Mean of values within each group. + + Examples + -------- + + >>> ser = pd.Series([1, 2, 3, 4], index=pd.DatetimeIndex( + ... ['2023-01-01', '2023-01-15', '2023-02-01', '2023-02-15'])) + >>> ser + 2023-01-01 1 + 2023-01-15 2 + 2023-02-01 3 + 2023-02-15 4 + dtype: int64 + >>> ser.resample('M').mean() + 2023-01-31 1.5 + 2023-02-28 3.5 + Freq: M, dtype: float64 """ maybe_warn_args_and_kwargs(type(self), "mean", args, kwargs) nv.validate_resampler_func("mean", args, kwargs) From beb759c67eca44b80d0042191d3173d7dff6f7b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dea=20Mar=C3=ADa=20L=C3=A9on?= Date: Wed, 28 Jun 2023 17:48:51 +0200 Subject: [PATCH 2/4] Example Resampler.min --- ci/code_checks.sh | 1 - pandas/core/resample.py | 19 +++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/ci/code_checks.sh b/ci/code_checks.sh index 5b6a065fb7bb5..e9087c0b2898c 100755 --- a/ci/code_checks.sh +++ b/ci/code_checks.sh @@ -125,7 +125,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then pandas.core.resample.Resampler.indices \ pandas.core.resample.Resampler.get_group \ pandas.core.resample.Resampler.ffill \ - pandas.core.resample.Resampler.min \ pandas.core.resample.Resampler.ohlc \ pandas.core.resample.Resampler.prod \ pandas.core.resample.Resampler.size \ diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 2b60b9ec4c769..3a143ab684de1 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -1080,6 +1080,25 @@ def min( *args, **kwargs, ): + """ + Compute min value of group. + + Examples + -------- + >>> ser = pd.Series([1, 2, 3, 4], index=pd.DatetimeIndex( + ... ['2023-01-01', '2023-01-15', '2023-02-01', '2023-02-15'])) + >>> ser + 2023-01-01 1 + 2023-01-15 2 + 2023-02-01 3 + 2023-02-15 4 + dtype: int64 + >>> ser.resample('M').min() + 2023-01-31 1 + 2023-02-28 3 + Freq: M, dtype: int64 + """ + maybe_warn_args_and_kwargs(type(self), "min", args, kwargs) nv.validate_resampler_func("min", args, kwargs) return self._downsample("min", numeric_only=numeric_only, min_count=min_count) From cf143748c75e2665bdab04f850785bdc8e5b6e23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dea=20Mar=C3=ADa=20L=C3=A9on?= Date: Wed, 28 Jun 2023 19:29:53 +0200 Subject: [PATCH 3/4] Added Returns section to min and max --- pandas/core/resample.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 3a143ab684de1..55bb1d26f9501 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -1083,6 +1083,10 @@ def min( """ Compute min value of group. + Returns + ------- + Series or DataFrame + Examples -------- >>> ser = pd.Series([1, 2, 3, 4], index=pd.DatetimeIndex( @@ -1113,6 +1117,10 @@ def max( """ Compute max value of group. + Returns + ------- + Series or DataFrame + Examples -------- >>> ser = pd.Series([1, 2, 3, 4], index=pd.DatetimeIndex( From 7f397a5a706eec96425eb53b5ae1232f41a5622d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dea=20Mar=C3=ADa=20L=C3=A9on?= Date: Thu, 29 Jun 2023 14:18:30 +0200 Subject: [PATCH 4/4] Changed M to MS and examples results accordingly --- pandas/core/groupby/generic.py | 8 ++++---- pandas/core/groupby/groupby.py | 16 ++++++++-------- pandas/core/resample.py | 32 ++++++++++++++++---------------- 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/pandas/core/groupby/generic.py b/pandas/core/groupby/generic.py index 1970abe38f289..614ea9464b968 100644 --- a/pandas/core/groupby/generic.py +++ b/pandas/core/groupby/generic.py @@ -658,10 +658,10 @@ def nunique(self, dropna: bool = True) -> Series | DataFrame: 2023-02-01 3 2023-02-15 3 dtype: int64 - >>> ser.resample('M').nunique() - 2023-01-31 2 - 2023-02-28 1 - Freq: M, dtype: int64 + >>> ser.resample('MS').nunique() + 2023-01-01 2 + 2023-02-01 1 + Freq: MS, dtype: int64 """ ids, _, _ = self.grouper.group_info diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index 5534cddc6f490..40507f756a3d9 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -2178,10 +2178,10 @@ def count(self) -> NDFrameT: 2023-02-01 3 2023-02-15 4 dtype: int64 - >>> ser.resample('M').count() - 2023-01-31 2 - 2023-02-28 2 - Freq: M, dtype: int64 + >>> ser.resample('MS').count() + 2023-01-01 2 + 2023-02-01 2 + Freq: MS, dtype: int64 """ data = self._get_data_to_aggregate() ids, _, ngroups = self.grouper.group_info @@ -2375,10 +2375,10 @@ def median(self, numeric_only: bool = False): ... '2023-02-01', ... '2023-02-10', ... '2023-02-15'])) - >>> ser.resample('M').median() - 2023-01-31 2.0 - 2023-02-28 4.0 - Freq: M, dtype: float64 + >>> ser.resample('MS').median() + 2023-01-01 2.0 + 2023-02-01 4.0 + Freq: MS, dtype: float64 """ result = self._cython_agg_general( "median", diff --git a/pandas/core/resample.py b/pandas/core/resample.py index 807c67857d309..2bc24260fc124 100644 --- a/pandas/core/resample.py +++ b/pandas/core/resample.py @@ -1091,10 +1091,10 @@ def asfreq(self, fill_value=None): 2023-02-01 3 2023-02-28 4 dtype: int64 - >>> ser.resample('M').asfreq() - 2023-01-31 2 - 2023-02-28 4 - Freq: M, dtype: int64 + >>> ser.resample('MS').asfreq() + 2023-01-01 1 + 2023-02-01 3 + Freq: MS, dtype: int64 """ return self._upsample("asfreq", fill_value=fill_value) @@ -1145,10 +1145,10 @@ def min( 2023-02-01 3 2023-02-15 4 dtype: int64 - >>> ser.resample('M').min() - 2023-01-31 1 - 2023-02-28 3 - Freq: M, dtype: int64 + >>> ser.resample('MS').min() + 2023-01-01 1 + 2023-02-01 3 + Freq: MS, dtype: int64 """ maybe_warn_args_and_kwargs(type(self), "min", args, kwargs) @@ -1179,10 +1179,10 @@ def max( 2023-02-01 3 2023-02-15 4 dtype: int64 - >>> ser.resample('M').max() - 2023-01-31 2 - 2023-02-28 4 - Freq: M, dtype: int64 + >>> ser.resample('MS').max() + 2023-01-01 2 + 2023-02-01 4 + Freq: MS, dtype: int64 """ maybe_warn_args_and_kwargs(type(self), "max", args, kwargs) nv.validate_resampler_func("max", args, kwargs) @@ -1252,10 +1252,10 @@ def mean( 2023-02-01 3 2023-02-15 4 dtype: int64 - >>> ser.resample('M').mean() - 2023-01-31 1.5 - 2023-02-28 3.5 - Freq: M, dtype: float64 + >>> ser.resample('MS').mean() + 2023-01-01 1.5 + 2023-02-01 3.5 + Freq: MS, dtype: float64 """ maybe_warn_args_and_kwargs(type(self), "mean", args, kwargs) nv.validate_resampler_func("mean", args, kwargs)