Skip to content

Commit bc7b394

Browse files
authored
DOC: Fixing EX01 - Added examples (#53564)
* Example for pct_change * Added examples for groupby sem, shift, size * Updated code_checks * Corrected error on groupby size
1 parent bdfaca6 commit bc7b394

File tree

2 files changed

+144
-10
lines changed

2 files changed

+144
-10
lines changed

ci/code_checks.sh

-8
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
267267
pandas.core.groupby.DataFrameGroupBy.ffill \
268268
pandas.core.groupby.DataFrameGroupBy.median \
269269
pandas.core.groupby.DataFrameGroupBy.ohlc \
270-
pandas.core.groupby.DataFrameGroupBy.pct_change \
271-
pandas.core.groupby.DataFrameGroupBy.sem \
272-
pandas.core.groupby.DataFrameGroupBy.shift \
273-
pandas.core.groupby.DataFrameGroupBy.size \
274270
pandas.core.groupby.DataFrameGroupBy.skew \
275271
pandas.core.groupby.DataFrameGroupBy.std \
276272
pandas.core.groupby.DataFrameGroupBy.var \
@@ -280,10 +276,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
280276
pandas.core.groupby.SeriesGroupBy.median \
281277
pandas.core.groupby.SeriesGroupBy.nunique \
282278
pandas.core.groupby.SeriesGroupBy.ohlc \
283-
pandas.core.groupby.SeriesGroupBy.pct_change \
284-
pandas.core.groupby.SeriesGroupBy.sem \
285-
pandas.core.groupby.SeriesGroupBy.shift \
286-
pandas.core.groupby.SeriesGroupBy.size \
287279
pandas.core.groupby.SeriesGroupBy.skew \
288280
pandas.core.groupby.SeriesGroupBy.std \
289281
pandas.core.groupby.SeriesGroupBy.var \

pandas/core/groupby/groupby.py

+144-2
Original file line numberDiff line numberDiff line change
@@ -2509,6 +2509,40 @@ def sem(self, ddof: int = 1, numeric_only: bool = False):
25092509
-------
25102510
Series or DataFrame
25112511
Standard error of the mean of values within each group.
2512+
2513+
Examples
2514+
--------
2515+
For SeriesGroupBy:
2516+
2517+
>>> lst = ['a', 'a', 'b', 'b']
2518+
>>> ser = pd.Series([5, 10, 8, 14], index=lst)
2519+
>>> ser
2520+
a 5
2521+
a 10
2522+
b 8
2523+
b 14
2524+
dtype: int64
2525+
>>> ser.groupby(level=0).sem()
2526+
a 2.5
2527+
b 3.0
2528+
dtype: float64
2529+
2530+
For DataFrameGroupBy:
2531+
2532+
>>> data = [[1, 12, 11], [1, 15, 2], [2, 5, 8], [2, 6, 12]]
2533+
>>> df = pd.DataFrame(data, columns=["a", "b", "c"],
2534+
... index=["tuna", "salmon", "catfish", "goldfish"])
2535+
>>> df
2536+
a b c
2537+
tuna 1 12 11
2538+
salmon 1 15 2
2539+
catfish 2 5 8
2540+
goldfish 2 6 12
2541+
>>> df.groupby("a").sem()
2542+
b c
2543+
a
2544+
1 1.5 4.5
2545+
2 0.5 2.0
25122546
"""
25132547
if numeric_only and self.obj.ndim == 1 and not is_numeric_dtype(self.obj.dtype):
25142548
raise TypeError(
@@ -2524,7 +2558,7 @@ def sem(self, ddof: int = 1, numeric_only: bool = False):
25242558

25252559
@final
25262560
@Substitution(name="groupby")
2527-
@Appender(_common_see_also)
2561+
@Substitution(see_also=_common_see_also)
25282562
def size(self) -> DataFrame | Series:
25292563
"""
25302564
Compute group sizes.
@@ -2534,6 +2568,37 @@ def size(self) -> DataFrame | Series:
25342568
DataFrame or Series
25352569
Number of rows in each group as a Series if as_index is True
25362570
or a DataFrame if as_index is False.
2571+
%(see_also)s
2572+
Examples
2573+
--------
2574+
2575+
For SeriesGroupBy:
2576+
2577+
>>> lst = ['a', 'a', 'b']
2578+
>>> ser = pd.Series([1, 2, 3], index=lst)
2579+
>>> ser
2580+
a 1
2581+
a 2
2582+
b 3
2583+
dtype: int64
2584+
>>> ser.groupby(level=0).size()
2585+
a 2
2586+
b 1
2587+
dtype: int64
2588+
2589+
>>> data = [[1, 2, 3], [1, 5, 6], [7, 8, 9]]
2590+
>>> df = pd.DataFrame(data, columns=["a", "b", "c"],
2591+
... index=["owl", "toucan", "eagle"])
2592+
>>> df
2593+
a b c
2594+
owl 1 2 3
2595+
toucan 1 5 6
2596+
eagle 7 8 9
2597+
>>> df.groupby("a").size()
2598+
a
2599+
1 2
2600+
7 1
2601+
dtype: int64
25372602
"""
25382603
result = self.grouper.size()
25392604

@@ -4439,6 +4504,44 @@ def shift(
44394504
See Also
44404505
--------
44414506
Index.shift : Shift values of Index.
4507+
4508+
Examples
4509+
--------
4510+
4511+
For SeriesGroupBy:
4512+
4513+
>>> lst = ['a', 'a', 'b', 'b']
4514+
>>> ser = pd.Series([1, 2, 3, 4], index=lst)
4515+
>>> ser
4516+
a 1
4517+
a 2
4518+
b 3
4519+
b 4
4520+
dtype: int64
4521+
>>> ser.groupby(level=0).shift(1)
4522+
a NaN
4523+
a 1.0
4524+
b NaN
4525+
b 3.0
4526+
dtype: float64
4527+
4528+
For DataFrameGroupBy:
4529+
4530+
>>> data = [[1, 2, 3], [1, 5, 6], [2, 5, 8], [2, 6, 9]]
4531+
>>> df = pd.DataFrame(data, columns=["a", "b", "c"],
4532+
... index=["tuna", "salmon", "catfish", "goldfish"])
4533+
>>> df
4534+
a b c
4535+
tuna 1 2 3
4536+
salmon 1 5 6
4537+
catfish 2 5 8
4538+
goldfish 2 6 9
4539+
>>> df.groupby("a").shift(1)
4540+
b c
4541+
tuna NaN NaN
4542+
salmon 2.0 3.0
4543+
catfish NaN NaN
4544+
goldfish 5.0 8.0
44424545
"""
44434546
if axis is not lib.no_default:
44444547
axis = self.obj._get_axis_number(axis)
@@ -4519,7 +4622,7 @@ def diff(
45194622

45204623
@final
45214624
@Substitution(name="groupby")
4522-
@Appender(_common_see_also)
4625+
@Substitution(see_also=_common_see_also)
45234626
def pct_change(
45244627
self,
45254628
periods: int = 1,
@@ -4535,7 +4638,46 @@ def pct_change(
45354638
-------
45364639
Series or DataFrame
45374640
Percentage changes within each group.
4641+
%(see_also)s
4642+
Examples
4643+
--------
4644+
4645+
For SeriesGroupBy:
4646+
4647+
>>> lst = ['a', 'a', 'b', 'b']
4648+
>>> ser = pd.Series([1, 2, 3, 4], index=lst)
4649+
>>> ser
4650+
a 1
4651+
a 2
4652+
b 3
4653+
b 4
4654+
dtype: int64
4655+
>>> ser.groupby(level=0).pct_change()
4656+
a NaN
4657+
a 1.000000
4658+
b NaN
4659+
b 0.333333
4660+
dtype: float64
4661+
4662+
For DataFrameGroupBy:
4663+
4664+
>>> data = [[1, 2, 3], [1, 5, 6], [2, 5, 8], [2, 6, 9]]
4665+
>>> df = pd.DataFrame(data, columns=["a", "b", "c"],
4666+
... index=["tuna", "salmon", "catfish", "goldfish"])
4667+
>>> df
4668+
a b c
4669+
tuna 1 2 3
4670+
salmon 1 5 6
4671+
catfish 2 5 8
4672+
goldfish 2 6 9
4673+
>>> df.groupby("a").pct_change()
4674+
b c
4675+
tuna NaN NaN
4676+
salmon 1.5 1.000
4677+
catfish NaN NaN
4678+
goldfish 0.2 0.125
45384679
"""
4680+
45394681
if axis is not lib.no_default:
45404682
axis = self.obj._get_axis_number(axis)
45414683
self._deprecate_axis(axis, "pct_change")

0 commit comments

Comments
 (0)