Skip to content

Commit

Permalink
DOC: Fixing EX01 - Added examples (pandas-dev#53564)
Browse files Browse the repository at this point in the history
* Example for pct_change

* Added examples for groupby sem, shift, size

* Updated code_checks

* Corrected error on groupby size
  • Loading branch information
DeaMariaLeon authored and root committed Jun 23, 2023
1 parent 0a8efaf commit c77f0df
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 10 deletions.
8 changes: 0 additions & 8 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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 \
Expand Down
146 changes: 144 additions & 2 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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.
Expand All @@ -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()

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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")
Expand Down

0 comments on commit c77f0df

Please sign in to comment.