diff --git a/ci/code_checks.sh b/ci/code_checks.sh index f2d6a2b222a3c..191e0d03b3a1a 100755 --- a/ci/code_checks.sh +++ b/ci/code_checks.sh @@ -267,10 +267,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then pandas.core.groupby.DataFrameGroupBy.ffill \ pandas.core.groupby.DataFrameGroupBy.median \ pandas.core.groupby.DataFrameGroupBy.ohlc \ - pandas.core.groupby.DataFrameGroupBy.pct_change \ - pandas.core.groupby.DataFrameGroupBy.sem \ - pandas.core.groupby.DataFrameGroupBy.shift \ - pandas.core.groupby.DataFrameGroupBy.size \ pandas.core.groupby.DataFrameGroupBy.skew \ pandas.core.groupby.DataFrameGroupBy.std \ pandas.core.groupby.DataFrameGroupBy.var \ @@ -280,10 +276,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then pandas.core.groupby.SeriesGroupBy.median \ pandas.core.groupby.SeriesGroupBy.nunique \ pandas.core.groupby.SeriesGroupBy.ohlc \ - pandas.core.groupby.SeriesGroupBy.pct_change \ - pandas.core.groupby.SeriesGroupBy.sem \ - pandas.core.groupby.SeriesGroupBy.shift \ - pandas.core.groupby.SeriesGroupBy.size \ pandas.core.groupby.SeriesGroupBy.skew \ pandas.core.groupby.SeriesGroupBy.std \ pandas.core.groupby.SeriesGroupBy.var \ diff --git a/pandas/core/groupby/groupby.py b/pandas/core/groupby/groupby.py index c96310c51b2a2..c372235481614 100644 --- a/pandas/core/groupby/groupby.py +++ b/pandas/core/groupby/groupby.py @@ -2509,6 +2509,40 @@ def sem(self, ddof: int = 1, numeric_only: bool = False): ------- Series or DataFrame Standard error of the mean of values within each group. + + Examples + -------- + For SeriesGroupBy: + + >>> lst = ['a', 'a', 'b', 'b'] + >>> ser = pd.Series([5, 10, 8, 14], index=lst) + >>> ser + a 5 + a 10 + b 8 + b 14 + dtype: int64 + >>> ser.groupby(level=0).sem() + a 2.5 + b 3.0 + dtype: float64 + + For DataFrameGroupBy: + + >>> data = [[1, 12, 11], [1, 15, 2], [2, 5, 8], [2, 6, 12]] + >>> df = pd.DataFrame(data, columns=["a", "b", "c"], + ... index=["tuna", "salmon", "catfish", "goldfish"]) + >>> df + a b c + tuna 1 12 11 + salmon 1 15 2 + catfish 2 5 8 + goldfish 2 6 12 + >>> df.groupby("a").sem() + b c + a + 1 1.5 4.5 + 2 0.5 2.0 """ if numeric_only and self.obj.ndim == 1 and not is_numeric_dtype(self.obj.dtype): raise TypeError( @@ -2524,7 +2558,7 @@ def sem(self, ddof: int = 1, numeric_only: bool = False): @final @Substitution(name="groupby") - @Appender(_common_see_also) + @Substitution(see_also=_common_see_also) def size(self) -> DataFrame | Series: """ Compute group sizes. @@ -2534,6 +2568,37 @@ def size(self) -> DataFrame | Series: DataFrame or Series Number of rows in each group as a Series if as_index is True or a DataFrame if as_index is False. + %(see_also)s + Examples + -------- + + For SeriesGroupBy: + + >>> lst = ['a', 'a', 'b'] + >>> ser = pd.Series([1, 2, 3], index=lst) + >>> ser + a 1 + a 2 + b 3 + dtype: int64 + >>> ser.groupby(level=0).size() + a 2 + b 1 + dtype: int64 + + >>> data = [[1, 2, 3], [1, 5, 6], [7, 8, 9]] + >>> df = pd.DataFrame(data, columns=["a", "b", "c"], + ... index=["owl", "toucan", "eagle"]) + >>> df + a b c + owl 1 2 3 + toucan 1 5 6 + eagle 7 8 9 + >>> df.groupby("a").size() + a + 1 2 + 7 1 + dtype: int64 """ result = self.grouper.size() @@ -4439,6 +4504,44 @@ def shift( See Also -------- Index.shift : Shift values of Index. + + Examples + -------- + + For SeriesGroupBy: + + >>> lst = ['a', 'a', 'b', 'b'] + >>> ser = pd.Series([1, 2, 3, 4], index=lst) + >>> ser + a 1 + a 2 + b 3 + b 4 + dtype: int64 + >>> ser.groupby(level=0).shift(1) + a NaN + a 1.0 + b NaN + b 3.0 + dtype: float64 + + For DataFrameGroupBy: + + >>> data = [[1, 2, 3], [1, 5, 6], [2, 5, 8], [2, 6, 9]] + >>> df = pd.DataFrame(data, columns=["a", "b", "c"], + ... index=["tuna", "salmon", "catfish", "goldfish"]) + >>> df + a b c + tuna 1 2 3 + salmon 1 5 6 + catfish 2 5 8 + goldfish 2 6 9 + >>> df.groupby("a").shift(1) + b c + tuna NaN NaN + salmon 2.0 3.0 + catfish NaN NaN + goldfish 5.0 8.0 """ if axis is not lib.no_default: axis = self.obj._get_axis_number(axis) @@ -4519,7 +4622,7 @@ def diff( @final @Substitution(name="groupby") - @Appender(_common_see_also) + @Substitution(see_also=_common_see_also) def pct_change( self, periods: int = 1, @@ -4535,7 +4638,46 @@ def pct_change( ------- Series or DataFrame Percentage changes within each group. + %(see_also)s + Examples + -------- + + For SeriesGroupBy: + + >>> lst = ['a', 'a', 'b', 'b'] + >>> ser = pd.Series([1, 2, 3, 4], index=lst) + >>> ser + a 1 + a 2 + b 3 + b 4 + dtype: int64 + >>> ser.groupby(level=0).pct_change() + a NaN + a 1.000000 + b NaN + b 0.333333 + dtype: float64 + + For DataFrameGroupBy: + + >>> data = [[1, 2, 3], [1, 5, 6], [2, 5, 8], [2, 6, 9]] + >>> df = pd.DataFrame(data, columns=["a", "b", "c"], + ... index=["tuna", "salmon", "catfish", "goldfish"]) + >>> df + a b c + tuna 1 2 3 + salmon 1 5 6 + catfish 2 5 8 + goldfish 2 6 9 + >>> df.groupby("a").pct_change() + b c + tuna NaN NaN + salmon 1.5 1.000 + catfish NaN NaN + goldfish 0.2 0.125 """ + if axis is not lib.no_default: axis = self.obj._get_axis_number(axis) self._deprecate_axis(axis, "pct_change")