@@ -720,6 +720,33 @@ def __repr__(self) -> str:
720
720
def groups (self ) -> dict [Hashable , np .ndarray ]:
721
721
"""
722
722
Dict {group name -> group labels}.
723
+
724
+ Examples
725
+ --------
726
+
727
+ For SeriesGroupBy:
728
+
729
+ >>> lst = ['a', 'a', 'b']
730
+ >>> ser = pd.Series([1, 2, 3], index=lst)
731
+ >>> ser
732
+ a 1
733
+ a 2
734
+ b 3
735
+ dtype: int64
736
+ >>> ser.groupby(level=0).groups
737
+ {'a': ['a', 'a'], 'b': ['b']}
738
+
739
+ For DataFrameGroupBy:
740
+
741
+ >>> data = [[1, 2, 3], [1, 5, 6], [7, 8, 9]]
742
+ >>> df = pd.DataFrame(data, columns=["a", "b", "c"])
743
+ >>> df
744
+ a b c
745
+ 0 1 2 3
746
+ 1 1 5 6
747
+ 2 7 8 9
748
+ >>> df.groupby(by=["a"]).groups
749
+ {1: [0, 1], 7: [2]}
723
750
"""
724
751
return self .grouper .groups
725
752
@@ -733,6 +760,34 @@ def ngroups(self) -> int:
733
760
def indices (self ) -> dict [Hashable , npt .NDArray [np .intp ]]:
734
761
"""
735
762
Dict {group name -> group indices}.
763
+
764
+ Examples
765
+ --------
766
+
767
+ For SeriesGroupBy:
768
+
769
+ >>> lst = ['a', 'a', 'b']
770
+ >>> ser = pd.Series([1, 2, 3], index=lst)
771
+ >>> ser
772
+ a 1
773
+ a 2
774
+ b 3
775
+ dtype: int64
776
+ >>> ser.groupby(level=0).indices
777
+ {'a': array([0, 1]), 'b': array([2])}
778
+
779
+ For DataFrameGroupBy:
780
+
781
+ >>> data = [[1, 2, 3], [1, 5, 6], [7, 8, 9]]
782
+ >>> df = pd.DataFrame(data, columns=["a", "b", "c"],
783
+ ... index=["owl", "toucan", "eagle"])
784
+ >>> df
785
+ a b c
786
+ owl 1 2 3
787
+ toucan 1 5 6
788
+ eagle 7 8 9
789
+ >>> df.groupby(by=["a"]).indices
790
+ {1: array([0, 1]), 7: array([2])}
736
791
"""
737
792
return self .grouper .indices
738
793
@@ -867,6 +922,38 @@ def get_group(self, name, obj=None) -> DataFrame | Series:
867
922
Returns
868
923
-------
869
924
same type as obj
925
+
926
+ Examples
927
+ --------
928
+
929
+ For SeriesGroupBy:
930
+
931
+ >>> lst = ['a', 'a', 'b']
932
+ >>> ser = pd.Series([1, 2, 3], index=lst)
933
+ >>> ser
934
+ a 1
935
+ a 2
936
+ b 3
937
+ dtype: int64
938
+ >>> ser.groupby(level=0).get_group("a")
939
+ a 1
940
+ a 2
941
+ dtype: int64
942
+
943
+ For DataFrameGroupBy:
944
+
945
+ >>> data = [[1, 2, 3], [1, 5, 6], [7, 8, 9]]
946
+ >>> df = pd.DataFrame(data, columns=["a", "b", "c"],
947
+ ... index=["owl", "toucan", "eagle"])
948
+ >>> df
949
+ a b c
950
+ owl 1 2 3
951
+ toucan 1 5 6
952
+ eagle 7 8 9
953
+ >>> df.groupby(by=["a"]).get_group(1)
954
+ a b c
955
+ owl 1 2 3
956
+ toucan 1 5 6
870
957
"""
871
958
if obj is None :
872
959
obj = self ._selected_obj
@@ -886,6 +973,47 @@ def __iter__(self) -> Iterator[tuple[Hashable, NDFrameT]]:
886
973
-------
887
974
Generator yielding sequence of (name, subsetted object)
888
975
for each group
976
+
977
+ Examples
978
+ --------
979
+
980
+ For SeriesGroupBy:
981
+
982
+ >>> lst = ['a', 'a', 'b']
983
+ >>> ser = pd.Series([1, 2, 3], index=lst)
984
+ >>> ser
985
+ a 1
986
+ a 2
987
+ b 3
988
+ dtype: int64
989
+ >>> for x, y in ser.groupby(level=0):
990
+ ... print(f'{x}\\ n{y}\\ n')
991
+ a
992
+ a 1
993
+ a 2
994
+ dtype: int64
995
+ b
996
+ b 3
997
+ dtype: int64
998
+
999
+ For DataFrameGroupBy:
1000
+
1001
+ >>> data = [[1, 2, 3], [1, 5, 6], [7, 8, 9]]
1002
+ >>> df = pd.DataFrame(data, columns=["a", "b", "c"])
1003
+ >>> df
1004
+ a b c
1005
+ 0 1 2 3
1006
+ 1 1 5 6
1007
+ 2 7 8 9
1008
+ >>> for x, y in df.groupby(by=["a"]):
1009
+ ... print(f'{x}\\ n{y}\\ n')
1010
+ (1,)
1011
+ a b c
1012
+ 0 1 2 3
1013
+ 1 1 5 6
1014
+ (7,)
1015
+ a b c
1016
+ 2 7 8 9
889
1017
"""
890
1018
keys = self .keys
891
1019
level = self .level
@@ -1787,7 +1915,7 @@ def _obj_1d_constructor(self) -> Callable:
1787
1915
1788
1916
@final
1789
1917
@Substitution (name = "groupby" )
1790
- @Appender ( _common_see_also )
1918
+ @Substitution ( see_also = _common_see_also )
1791
1919
def any (self , skipna : bool = True ):
1792
1920
"""
1793
1921
Return True if any value in the group is truthful, else False.
@@ -1802,6 +1930,38 @@ def any(self, skipna: bool = True):
1802
1930
Series or DataFrame
1803
1931
DataFrame or Series of boolean values, where a value is True if any element
1804
1932
is True within its respective group, False otherwise.
1933
+ %(see_also)s
1934
+ Examples
1935
+ --------
1936
+ For SeriesGroupBy:
1937
+
1938
+ >>> lst = ['a', 'a', 'b']
1939
+ >>> ser = pd.Series([1, 2, 0], index=lst)
1940
+ >>> ser
1941
+ a 1
1942
+ a 2
1943
+ b 0
1944
+ dtype: int64
1945
+ >>> ser.groupby(level=0).any()
1946
+ a True
1947
+ b False
1948
+ dtype: bool
1949
+
1950
+ For DataFrameGroupBy:
1951
+
1952
+ >>> data = [[1, 0, 3], [1, 0, 6], [7, 1, 9]]
1953
+ >>> df = pd.DataFrame(data, columns=["a", "b", "c"],
1954
+ ... index=["ostrich", "penguin", "parrot"])
1955
+ >>> df
1956
+ a b c
1957
+ ostrich 1 0 3
1958
+ penguin 1 0 6
1959
+ parrot 7 1 9
1960
+ >>> df.groupby(by=["a"]).any()
1961
+ b c
1962
+ a
1963
+ 1 False True
1964
+ 7 True True
1805
1965
"""
1806
1966
return self ._cython_agg_general (
1807
1967
"any" ,
@@ -1811,7 +1971,7 @@ def any(self, skipna: bool = True):
1811
1971
1812
1972
@final
1813
1973
@Substitution (name = "groupby" )
1814
- @Appender ( _common_see_also )
1974
+ @Substitution ( see_also = _common_see_also )
1815
1975
def all (self , skipna : bool = True ):
1816
1976
"""
1817
1977
Return True if all values in the group are truthful, else False.
@@ -1826,6 +1986,39 @@ def all(self, skipna: bool = True):
1826
1986
Series or DataFrame
1827
1987
DataFrame or Series of boolean values, where a value is True if all elements
1828
1988
are True within its respective group, False otherwise.
1989
+ %(see_also)s
1990
+ Examples
1991
+ --------
1992
+
1993
+ For SeriesGroupBy:
1994
+
1995
+ >>> lst = ['a', 'a', 'b']
1996
+ >>> ser = pd.Series([1, 2, 0], index=lst)
1997
+ >>> ser
1998
+ a 1
1999
+ a 2
2000
+ b 0
2001
+ dtype: int64
2002
+ >>> ser.groupby(level=0).all()
2003
+ a True
2004
+ b False
2005
+ dtype: bool
2006
+
2007
+ For DataFrameGroupBy:
2008
+
2009
+ >>> data = [[1, 0, 3], [1, 5, 6], [7, 8, 9]]
2010
+ >>> df = pd.DataFrame(data, columns=["a", "b", "c"],
2011
+ ... index=["ostrich", "penguin", "parrot"])
2012
+ >>> df
2013
+ a b c
2014
+ ostrich 1 0 3
2015
+ penguin 1 5 6
2016
+ parrot 7 8 9
2017
+ >>> df.groupby(by=["a"]).all()
2018
+ b c
2019
+ a
2020
+ 1 False True
2021
+ 7 True True
1829
2022
"""
1830
2023
return self ._cython_agg_general (
1831
2024
"all" ,
0 commit comments