Skip to content
This repository was archived by the owner on Feb 2, 2024. It is now read-only.

Commit 3ca1bd6

Browse files
Updating docstrings and adding groupby examples (#751)
1 parent 488f6a3 commit 3ca1bd6

16 files changed

Lines changed: 753 additions & 23 deletions
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
.. _api_ref.pandas.groupby:
2+
.. include:: ./../ext_links.txt
3+
4+
Pandas Groupby
5+
=============
6+
.. currentmodule:: pandas.core.groupby
7+
8+
Indexing, iteration
9+
--------------------------------
10+
11+
.. sdc_toctree
12+
GroupBy.__iter__
13+
GroupBy.groups
14+
GroupBy.indices
15+
GroupBy.get_group
16+
Grouper
17+
18+
Function application
19+
-----------------------------------
20+
21+
.. sdc_toctree
22+
GroupBy.apply
23+
GroupBy.agg
24+
GroupBy.aggregate
25+
GroupBy.transform
26+
GroupBy.pipe
27+
28+
Computations / descriptive stats
29+
----------------------------------------------
30+
31+
.. sdc_toctree
32+
GroupBy.all
33+
GroupBy.any
34+
GroupBy.bfill
35+
GroupBy.count
36+
GroupBy.cumcount
37+
GroupBy.cummax
38+
GroupBy.cummin
39+
GroupBy.cumprod
40+
GroupBy.cumsum
41+
GroupBy.ffill
42+
GroupBy.first
43+
GroupBy.head
44+
GroupBy.last
45+
GroupBy.max
46+
GroupBy.mean
47+
GroupBy.median
48+
GroupBy.min
49+
GroupBy.ngroup
50+
GroupBy.nth
51+
GroupBy.ohlc
52+
GroupBy.prod
53+
GroupBy.rank
54+
GroupBy.pct_change
55+
GroupBy.size
56+
GroupBy.sem
57+
GroupBy.std
58+
GroupBy.sum
59+
GroupBy.var
60+
GroupBy.tail
61+
62+
The following methods are available in both ``SeriesGroupBy`` and ``DataFrameGroupBy`` objects, but may differ slightly,
63+
usually in that the ``DataFrameGroupBy`` version usually permits the specification of an axis argument,
64+
and often an argument indicating whether to restrict application to columns of a specific data type.
65+
66+
.. sdc_toctree
67+
DataFrameGroupBy.all
68+
DataFrameGroupBy.any
69+
DataFrameGroupBy.bfill
70+
DataFrameGroupBy.corr
71+
DataFrameGroupBy.count
72+
DataFrameGroupBy.cov
73+
DataFrameGroupBy.cummax
74+
DataFrameGroupBy.cummin
75+
DataFrameGroupBy.cumprod
76+
DataFrameGroupBy.cumsum
77+
DataFrameGroupBy.describe
78+
DataFrameGroupBy.diff
79+
DataFrameGroupBy.ffill
80+
DataFrameGroupBy.fillna
81+
DataFrameGroupBy.filter
82+
DataFrameGroupBy.hist
83+
DataFrameGroupBy.idxmax
84+
DataFrameGroupBy.idxmin
85+
DataFrameGroupBy.mad
86+
DataFrameGroupBy.nunique
87+
DataFrameGroupBy.pct_change
88+
DataFrameGroupBy.plot
89+
DataFrameGroupBy.quantile
90+
DataFrameGroupBy.rank
91+
DataFrameGroupBy.resample
92+
DataFrameGroupBy.shift
93+
DataFrameGroupBy.size
94+
DataFrameGroupBy.skew
95+
DataFrameGroupBy.take
96+
DataFrameGroupBy.tshift
97+
98+
The following methods are available only for ``SeriesGroupBy`` objects.
99+
100+
.. sdc_toctree
101+
SeriesGroupBy.nlargest
102+
SeriesGroupBy.nsmallest
103+
SeriesGroupBy.nunique
104+
SeriesGroupBy.unique
105+
SeriesGroupBy.value_counts
106+
SeriesGroupBy.is_monotonic_increasing
107+
SeriesGroupBy.is_monotonic_decreasing
108+
109+
The following methods are available only for DataFrameGroupBy objects.
110+
111+
.. sdc_toctree
112+
DataFrameGroupBy.corrwith
113+
DataFrameGroupBy.boxplot

‎docs/source/apireference.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ API Reference
1010
Series: Columnar Data Structure <./_api_ref/api_ref.pandas.series.rst>
1111
Dataframe: Tabular Data Structure <./_api_ref/api_ref.pandas.dataframe.rst>
1212
Window <./_api_ref/api_ref.pandas.window.rst>
13+
GroupBy <./_api_ref/api_ref.pandas.groupby.rst>

‎docs/source/buildscripts/apiref_generator.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
'./_templates/_api_ref.pandas.series_templ.rst',
4040
'./_templates/_api_ref.pandas.dataframe_templ.rst',
4141
'./_templates/_api_ref.pandas.window_templ.rst',
42+
'./_templates/_api_ref.pandas.groupby_templ.rst',
4243
]
4344

4445

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# *****************************************************************************
2+
# Copyright (c) 2020, Intel Corporation All rights reserved.
3+
#
4+
# Redistribution and use in source and binary forms, with or without
5+
# modification, are permitted provided that the following conditions are met:
6+
#
7+
# Redistributions of source code must retain the above copyright notice,
8+
# this list of conditions and the following disclaimer.
9+
#
10+
# Redistributions in binary form must reproduce the above copyright notice,
11+
# this list of conditions and the following disclaimer in the documentation
12+
# and/or other materials provided with the distribution.
13+
#
14+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17+
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
18+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19+
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20+
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21+
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22+
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23+
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
24+
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
# *****************************************************************************
26+
27+
import pandas as pd
28+
import numpy as np
29+
from numba import njit
30+
31+
32+
@njit
33+
def df_groupby_count():
34+
df = pd.DataFrame({'A': [1, 2, 3, 1, 2, 3, 3, 3, 2],
35+
'B': [0, 1, np.nan, np.nan, 2, 4, 3, 2, np.inf],
36+
'C': [np.nan, 2, 3, np.nan, 5, 6, 7, 8, 9]})
37+
out_df = df.groupby('A').count()
38+
39+
# Expect DataFrame of
40+
# {'B': [1, 3, 3], 'C': [0, 3, 4} with index=[1, 2, 3]
41+
return out_df
42+
43+
44+
print(df_groupby_count())
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# *****************************************************************************
2+
# Copyright (c) 2020, Intel Corporation All rights reserved.
3+
#
4+
# Redistribution and use in source and binary forms, with or without
5+
# modification, are permitted provided that the following conditions are met:
6+
#
7+
# Redistributions of source code must retain the above copyright notice,
8+
# this list of conditions and the following disclaimer.
9+
#
10+
# Redistributions in binary form must reproduce the above copyright notice,
11+
# this list of conditions and the following disclaimer in the documentation
12+
# and/or other materials provided with the distribution.
13+
#
14+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17+
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
18+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19+
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20+
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21+
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22+
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23+
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
24+
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
# *****************************************************************************
26+
27+
import pandas as pd
28+
from numba import njit
29+
30+
31+
@njit
32+
def df_groupby_max():
33+
df = pd.DataFrame({'A': [1, 2, 3, 1, 2, 3, 3, 3, 2],
34+
'B': [0, 1, 5, 0, 2, 4, 3, 2, 3],
35+
'C': [1, 2, 3, 4, 5, 6, 7, 8, 9]})
36+
out_df = df.groupby('A').max()
37+
38+
# Expect DataFrame of
39+
# {'B': [0, 3, 5], 'C': [4, 9, 8} with index=[1, 2, 3]
40+
return out_df
41+
42+
43+
print(df_groupby_max())
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# *****************************************************************************
2+
# Copyright (c) 2020, Intel Corporation All rights reserved.
3+
#
4+
# Redistribution and use in source and binary forms, with or without
5+
# modification, are permitted provided that the following conditions are met:
6+
#
7+
# Redistributions of source code must retain the above copyright notice,
8+
# this list of conditions and the following disclaimer.
9+
#
10+
# Redistributions in binary form must reproduce the above copyright notice,
11+
# this list of conditions and the following disclaimer in the documentation
12+
# and/or other materials provided with the distribution.
13+
#
14+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17+
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
18+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19+
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20+
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21+
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22+
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23+
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
24+
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
# *****************************************************************************
26+
27+
import pandas as pd
28+
from numba import njit
29+
30+
31+
@njit
32+
def df_groupby_mean():
33+
df = pd.DataFrame({'A': [1, 2, 3, 1, 2, 3, 3, 3, 2],
34+
'B': [0, 1, 5, 0, 2, 4, 3, 2, 3],
35+
'C': [1, 2, 3, 4, 5, 6, 7, 8, 9]})
36+
out_df = df.groupby('A').mean()
37+
38+
# Expect DataFrame of
39+
# {'B': [0.0, 2.0, 3.5], 'C': [2.500000, 5.333333, 6.000000} with index=[1, 2, 3]
40+
return out_df
41+
42+
43+
print(df_groupby_mean())
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# *****************************************************************************
2+
# Copyright (c) 2020, Intel Corporation All rights reserved.
3+
#
4+
# Redistribution and use in source and binary forms, with or without
5+
# modification, are permitted provided that the following conditions are met:
6+
#
7+
# Redistributions of source code must retain the above copyright notice,
8+
# this list of conditions and the following disclaimer.
9+
#
10+
# Redistributions in binary form must reproduce the above copyright notice,
11+
# this list of conditions and the following disclaimer in the documentation
12+
# and/or other materials provided with the distribution.
13+
#
14+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17+
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
18+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19+
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20+
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21+
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22+
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23+
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
24+
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
# *****************************************************************************
26+
27+
import pandas as pd
28+
from numba import njit
29+
30+
31+
@njit
32+
def df_groupby_median():
33+
df = pd.DataFrame({'A': [1, 2, 3, 1, 2, 3, 3, 3, 2],
34+
'B': [0, 1, 5, 0, 3, 4, 3, 2, 4],
35+
'C': [1, 2, 3, 4, 5, 6, 7, 8, 9]})
36+
out_df = df.groupby('A').median()
37+
38+
# Expect DataFrame of
39+
# {'B': [0.0, 3.0, 3.5], 'C': [2.5, 5.0, 6.5} with index=[1, 2, 3]
40+
return out_df
41+
42+
43+
print(df_groupby_median())
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# *****************************************************************************
2+
# Copyright (c) 2020, Intel Corporation All rights reserved.
3+
#
4+
# Redistribution and use in source and binary forms, with or without
5+
# modification, are permitted provided that the following conditions are met:
6+
#
7+
# Redistributions of source code must retain the above copyright notice,
8+
# this list of conditions and the following disclaimer.
9+
#
10+
# Redistributions in binary form must reproduce the above copyright notice,
11+
# this list of conditions and the following disclaimer in the documentation
12+
# and/or other materials provided with the distribution.
13+
#
14+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17+
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
18+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19+
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20+
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21+
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22+
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23+
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
24+
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
# *****************************************************************************
26+
27+
import pandas as pd
28+
from numba import njit
29+
30+
31+
@njit
32+
def df_groupby_min():
33+
df = pd.DataFrame({'A': [1, 2, 3, 1, 2, 3, 3, 3, 2],
34+
'B': [0, 1, 5, 0, 2, 4, 3, 2, 3],
35+
'C': [1, 2, 3, 4, 5, 6, 7, 8, 9]})
36+
out_df = df.groupby('A').min()
37+
38+
# Expect DataFrame of
39+
# {'B': [0, 1, 2], 'C': [1, 2, 3} with index=[1, 2, 3]
40+
return out_df
41+
42+
43+
print(df_groupby_min())
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# *****************************************************************************
2+
# Copyright (c) 2020, Intel Corporation All rights reserved.
3+
#
4+
# Redistribution and use in source and binary forms, with or without
5+
# modification, are permitted provided that the following conditions are met:
6+
#
7+
# Redistributions of source code must retain the above copyright notice,
8+
# this list of conditions and the following disclaimer.
9+
#
10+
# Redistributions in binary form must reproduce the above copyright notice,
11+
# this list of conditions and the following disclaimer in the documentation
12+
# and/or other materials provided with the distribution.
13+
#
14+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17+
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
18+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19+
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20+
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21+
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22+
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23+
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
24+
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25+
# *****************************************************************************
26+
27+
import pandas as pd
28+
from numba import njit
29+
30+
31+
@njit
32+
def df_groupby_prod():
33+
df = pd.DataFrame({'A': [1, 2, 3, 1, 2, 3, 3, 3, 2],
34+
'B': [0, 1, 5, 0, 2, 4, 3, 2, 3],
35+
'C': [1, 2, 3, 4, 5, 6, 7, 8, 9]})
36+
out_df = df.groupby('A').prod()
37+
38+
# Expect DataFrame of
39+
# {'B': [0, 6, 120], 'C': [4, 90, 1008} with index=[1, 2, 3]
40+
return out_df
41+
42+
43+
print(df_groupby_prod())

0 commit comments

Comments
 (0)