@@ -341,6 +341,10 @@ class providing the base-class of operations.
341
341
-------
342
342
Series or DataFrame
343
343
Computed {fname} of values within each group.
344
+
345
+ Examples
346
+ --------
347
+ {example}
344
348
"""
345
349
346
350
_pipe_template = """
@@ -2550,7 +2554,46 @@ def size(self) -> DataFrame | Series:
2550
2554
return result
2551
2555
2552
2556
@final
2553
- @doc (_groupby_agg_method_template , fname = "sum" , no = False , mc = 0 )
2557
+ @doc (
2558
+ _groupby_agg_method_template ,
2559
+ fname = "sum" ,
2560
+ no = False ,
2561
+ mc = 0 ,
2562
+ example = dedent (
2563
+ """\
2564
+ For SeriesGroupBy:
2565
+
2566
+ >>> lst = ['a', 'a', 'b', 'b']
2567
+ >>> ser = pd.Series([1, 2, 3, 4], index=lst)
2568
+ >>> ser
2569
+ a 1
2570
+ a 2
2571
+ b 3
2572
+ b 4
2573
+ dtype: int64
2574
+ >>> ser.groupby(level=0).sum()
2575
+ a 3
2576
+ b 7
2577
+ dtype: int64
2578
+
2579
+ For DataFrameGroupBy:
2580
+
2581
+ >>> data = [[1, 8, 2], [1, 2, 5], [2, 5, 8], [2, 6, 9]]
2582
+ >>> df = pd.DataFrame(data, columns=["a", "b", "c"],
2583
+ ... index=["tiger", "leopard", "cheetah", "lion"])
2584
+ >>> df
2585
+ a b c
2586
+ tiger 1 8 2
2587
+ leopard 1 2 5
2588
+ cheetah 2 5 8
2589
+ lion 2 6 9
2590
+ >>> df.groupby("a").sum()
2591
+ b c
2592
+ a
2593
+ 1 10 7
2594
+ 2 11 17"""
2595
+ ),
2596
+ )
2554
2597
def sum (
2555
2598
self ,
2556
2599
numeric_only : bool = False ,
@@ -2580,14 +2623,92 @@ def sum(
2580
2623
return self ._reindex_output (result , fill_value = 0 )
2581
2624
2582
2625
@final
2583
- @doc (_groupby_agg_method_template , fname = "prod" , no = False , mc = 0 )
2626
+ @doc (
2627
+ _groupby_agg_method_template ,
2628
+ fname = "prod" ,
2629
+ no = False ,
2630
+ mc = 0 ,
2631
+ example = dedent (
2632
+ """\
2633
+ For SeriesGroupBy:
2634
+
2635
+ >>> lst = ['a', 'a', 'b', 'b']
2636
+ >>> ser = pd.Series([1, 2, 3, 4], index=lst)
2637
+ >>> ser
2638
+ a 1
2639
+ a 2
2640
+ b 3
2641
+ b 4
2642
+ dtype: int64
2643
+ >>> ser.groupby(level=0).prod()
2644
+ a 2
2645
+ b 12
2646
+ dtype: int64
2647
+
2648
+ For DataFrameGroupBy:
2649
+
2650
+ >>> data = [[1, 8, 2], [1, 2, 5], [2, 5, 8], [2, 6, 9]]
2651
+ >>> df = pd.DataFrame(data, columns=["a", "b", "c"],
2652
+ ... index=["tiger", "leopard", "cheetah", "lion"])
2653
+ >>> df
2654
+ a b c
2655
+ tiger 1 8 2
2656
+ leopard 1 2 5
2657
+ cheetah 2 5 8
2658
+ lion 2 6 9
2659
+ >>> df.groupby("a").prod()
2660
+ b c
2661
+ a
2662
+ 1 16 10
2663
+ 2 30 72"""
2664
+ ),
2665
+ )
2584
2666
def prod (self , numeric_only : bool = False , min_count : int = 0 ):
2585
2667
return self ._agg_general (
2586
2668
numeric_only = numeric_only , min_count = min_count , alias = "prod" , npfunc = np .prod
2587
2669
)
2588
2670
2589
2671
@final
2590
- @doc (_groupby_agg_method_template , fname = "min" , no = False , mc = - 1 )
2672
+ @doc (
2673
+ _groupby_agg_method_template ,
2674
+ fname = "min" ,
2675
+ no = False ,
2676
+ mc = - 1 ,
2677
+ example = dedent (
2678
+ """\
2679
+ For SeriesGroupBy:
2680
+
2681
+ >>> lst = ['a', 'a', 'b', 'b']
2682
+ >>> ser = pd.Series([1, 2, 3, 4], index=lst)
2683
+ >>> ser
2684
+ a 1
2685
+ a 2
2686
+ b 3
2687
+ b 4
2688
+ dtype: int64
2689
+ >>> ser.groupby(level=0).min()
2690
+ a 1
2691
+ b 3
2692
+ dtype: int64
2693
+
2694
+ For DataFrameGroupBy:
2695
+
2696
+ >>> data = [[1, 8, 2], [1, 2, 5], [2, 5, 8], [2, 6, 9]]
2697
+ >>> df = pd.DataFrame(data, columns=["a", "b", "c"],
2698
+ ... index=["tiger", "leopard", "cheetah", "lion"])
2699
+ >>> df
2700
+ a b c
2701
+ tiger 1 8 2
2702
+ leopard 1 2 5
2703
+ cheetah 2 5 8
2704
+ lion 2 6 9
2705
+ >>> df.groupby("a").min()
2706
+ b c
2707
+ a
2708
+ 1 2 2
2709
+ 2 5 8"""
2710
+ ),
2711
+ )
2591
2712
def min (
2592
2713
self ,
2593
2714
numeric_only : bool = False ,
@@ -2608,7 +2729,46 @@ def min(
2608
2729
)
2609
2730
2610
2731
@final
2611
- @doc (_groupby_agg_method_template , fname = "max" , no = False , mc = - 1 )
2732
+ @doc (
2733
+ _groupby_agg_method_template ,
2734
+ fname = "max" ,
2735
+ no = False ,
2736
+ mc = - 1 ,
2737
+ example = dedent (
2738
+ """\
2739
+ For SeriesGroupBy:
2740
+
2741
+ >>> lst = ['a', 'a', 'b', 'b']
2742
+ >>> ser = pd.Series([1, 2, 3, 4], index=lst)
2743
+ >>> ser
2744
+ a 1
2745
+ a 2
2746
+ b 3
2747
+ b 4
2748
+ dtype: int64
2749
+ >>> ser.groupby(level=0).max()
2750
+ a 2
2751
+ b 4
2752
+ dtype: int64
2753
+
2754
+ For DataFrameGroupBy:
2755
+
2756
+ >>> data = [[1, 8, 2], [1, 2, 5], [2, 5, 8], [2, 6, 9]]
2757
+ >>> df = pd.DataFrame(data, columns=["a", "b", "c"],
2758
+ ... index=["tiger", "leopard", "cheetah", "lion"])
2759
+ >>> df
2760
+ a b c
2761
+ tiger 1 8 2
2762
+ leopard 1 2 5
2763
+ cheetah 2 5 8
2764
+ lion 2 6 9
2765
+ >>> df.groupby("a").max()
2766
+ b c
2767
+ a
2768
+ 1 8 5
2769
+ 2 6 9"""
2770
+ ),
2771
+ )
2612
2772
def max (
2613
2773
self ,
2614
2774
numeric_only : bool = False ,
0 commit comments