Skip to content

Commit cabe062

Browse files
committed
Refactored _doc_template to _common_see_also and used with the substitution decorator in docstrings where commonly repeated.
1 parent c73c5f0 commit cabe062

File tree

1 file changed

+32
-44
lines changed

1 file changed

+32
-44
lines changed

pandas/core/groupby/groupby.py

+32-44
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class providing the base-class of operations.
4141
from pandas.core.series import Series
4242
from pandas.core.sorting import get_group_index_sorter
4343

44-
_doc_template = """
44+
_common_see_also = """
4545
See Also
4646
--------
4747
pandas.Series.%(name)s
@@ -1044,7 +1044,7 @@ def result_to_bool(result):
10441044
val_test=val_test, skipna=skipna)
10451045

10461046
@Substitution(name='groupby')
1047-
@Appender(_doc_template)
1047+
@Appender(_common_see_also)
10481048
def any(self, skipna=True):
10491049
"""
10501050
Returns True if any value in the group is truthful, else False.
@@ -1057,7 +1057,7 @@ def any(self, skipna=True):
10571057
return self._bool_agg('any', skipna)
10581058

10591059
@Substitution(name='groupby')
1060-
@Appender(_doc_template)
1060+
@Appender(_common_see_also)
10611061
def all(self, skipna=True):
10621062
"""
10631063
Returns True if all values in the group are truthful, else False.
@@ -1070,7 +1070,7 @@ def all(self, skipna=True):
10701070
return self._bool_agg('all', skipna)
10711071

10721072
@Substitution(name='groupby')
1073-
@Appender(_doc_template)
1073+
@Appender(_common_see_also)
10741074
def count(self):
10751075
"""
10761076
Compute count of group, excluding missing values.
@@ -1079,7 +1079,8 @@ def count(self):
10791079
# defined here for API doc
10801080
raise NotImplementedError
10811081

1082-
@Substitution(name='groupby')
1082+
@Substitution(name='groupby',
1083+
see_also=_common_see_also)
10831084
def mean(self, *args, **kwargs):
10841085
"""
10851086
Compute mean of groups, excluding missing values.
@@ -1088,11 +1089,7 @@ def mean(self, *args, **kwargs):
10881089
-------
10891090
pandas.Series or pandas.DataFrame
10901091
1091-
See Also
1092-
--------
1093-
pandas.Series.%(name)s
1094-
pandas.DataFrame.%(name)s
1095-
pandas.Panel.%(name)s
1092+
%(see_also)s
10961093
10971094
Examples
10981095
--------
@@ -1142,7 +1139,7 @@ def mean(self, *args, **kwargs):
11421139
return self._python_agg_general(f)
11431140

11441141
@Substitution(name='groupby')
1145-
@Appender(_doc_template)
1142+
@Appender(_common_see_also)
11461143
def median(self, **kwargs):
11471144
"""
11481145
Compute median of groups, excluding missing values.
@@ -1163,7 +1160,7 @@ def f(x):
11631160
return self._python_agg_general(f)
11641161

11651162
@Substitution(name='groupby')
1166-
@Appender(_doc_template)
1163+
@Appender(_common_see_also)
11671164
def std(self, ddof=1, *args, **kwargs):
11681165
"""
11691166
Compute standard deviation of groups, excluding missing values.
@@ -1181,7 +1178,7 @@ def std(self, ddof=1, *args, **kwargs):
11811178
return np.sqrt(self.var(ddof=ddof, **kwargs))
11821179

11831180
@Substitution(name='groupby')
1184-
@Appender(_doc_template)
1181+
@Appender(_common_see_also)
11851182
def var(self, ddof=1, *args, **kwargs):
11861183
"""
11871184
Compute variance of groups, excluding missing values.
@@ -1207,7 +1204,7 @@ def var(self, ddof=1, *args, **kwargs):
12071204
return self._python_agg_general(f)
12081205

12091206
@Substitution(name='groupby')
1210-
@Appender(_doc_template)
1207+
@Appender(_common_see_also)
12111208
def sem(self, ddof=1):
12121209
"""
12131210
Compute standard error of the mean of groups, excluding missing values.
@@ -1223,7 +1220,7 @@ def sem(self, ddof=1):
12231220
return self.std(ddof=ddof) / np.sqrt(self.count())
12241221

12251222
@Substitution(name='groupby')
1226-
@Appender(_doc_template)
1223+
@Appender(_common_see_also)
12271224
def size(self):
12281225
"""
12291226
Compute group sizes.
@@ -1247,7 +1244,7 @@ def groupby_function(name, alias, npfunc,
12471244
_local_template = "Compute %(f)s of group values"
12481245

12491246
@Substitution(name='groupby', f=name)
1250-
@Appender(_doc_template)
1247+
@Appender(_common_see_also)
12511248
@Appender(_local_template)
12521249
def f(self, **kwargs):
12531250
if 'numeric_only' not in kwargs:
@@ -1312,7 +1309,7 @@ def last(x):
13121309
numeric_only=False)
13131310

13141311
@Substitution(name='groupby')
1315-
@Appender(_doc_template)
1312+
@Appender(_common_see_also)
13161313
def ohlc(self):
13171314
"""
13181315
Compute sum of values, excluding missing values.
@@ -1441,7 +1438,7 @@ def resample(self, rule, *args, **kwargs):
14411438
return get_resampler_for_grouping(self, rule, *args, **kwargs)
14421439

14431440
@Substitution(name='groupby')
1444-
@Appender(_doc_template)
1441+
@Appender(_common_see_also)
14451442
def rolling(self, *args, **kwargs):
14461443
"""
14471444
Return a rolling grouper, providing rolling functionality per group.
@@ -1450,7 +1447,7 @@ def rolling(self, *args, **kwargs):
14501447
return RollingGroupby(self, *args, **kwargs)
14511448

14521449
@Substitution(name='groupby')
1453-
@Appender(_doc_template)
1450+
@Appender(_common_see_also)
14541451
def expanding(self, *args, **kwargs):
14551452
"""
14561453
Return an expanding grouper, providing expanding
@@ -1532,7 +1529,8 @@ def backfill(self, limit=None):
15321529
return self._fill('bfill', limit=limit)
15331530
bfill = backfill
15341531

1535-
@Substitution(name='groupby')
1532+
@Substitution(name='groupby',
1533+
see_also=_common_see_also)
15361534
def nth(self, n, dropna=None):
15371535
"""
15381536
Take the nth row from each group if n is an int, or a subset of rows
@@ -1551,11 +1549,7 @@ def nth(self, n, dropna=None):
15511549
apply the specified dropna operation before counting which row is
15521550
the nth row. Needs to be None, 'any' or 'all'
15531551
1554-
See Also
1555-
--------
1556-
pandas.Series.%(name)s
1557-
pandas.DataFrame.%(name)s
1558-
pandas.Panel.%(name)s
1552+
%(see_also)s
15591553
15601554
Examples
15611555
--------
@@ -1818,7 +1812,7 @@ def cumcount(self, ascending=True):
18181812
return Series(cumcounts, index)
18191813

18201814
@Substitution(name='groupby')
1821-
@Appender(_doc_template)
1815+
@Appender(_common_see_also)
18221816
def rank(self, method='average', ascending=True, na_option='keep',
18231817
pct=False, axis=0):
18241818
"""
@@ -1855,7 +1849,7 @@ def rank(self, method='average', ascending=True, na_option='keep',
18551849
na_option=na_option, pct=pct, axis=axis)
18561850

18571851
@Substitution(name='groupby')
1858-
@Appender(_doc_template)
1852+
@Appender(_common_see_also)
18591853
def cumprod(self, axis=0, *args, **kwargs):
18601854
"""
18611855
Cumulative product for each group.
@@ -1868,7 +1862,7 @@ def cumprod(self, axis=0, *args, **kwargs):
18681862
return self._cython_transform('cumprod', **kwargs)
18691863

18701864
@Substitution(name='groupby')
1871-
@Appender(_doc_template)
1865+
@Appender(_common_see_also)
18721866
def cumsum(self, axis=0, *args, **kwargs):
18731867
"""
18741868
Cumulative sum for each group.
@@ -1881,7 +1875,7 @@ def cumsum(self, axis=0, *args, **kwargs):
18811875
return self._cython_transform('cumsum', **kwargs)
18821876

18831877
@Substitution(name='groupby')
1884-
@Appender(_doc_template)
1878+
@Appender(_common_see_also)
18851879
def cummin(self, axis=0, **kwargs):
18861880
"""
18871881
Cumulative min for each group.
@@ -1892,7 +1886,7 @@ def cummin(self, axis=0, **kwargs):
18921886
return self._cython_transform('cummin', numeric_only=False)
18931887

18941888
@Substitution(name='groupby')
1895-
@Appender(_doc_template)
1889+
@Appender(_common_see_also)
18961890
def cummax(self, axis=0, **kwargs):
18971891
"""
18981892
Cumulative max for each group.
@@ -2001,7 +1995,7 @@ def _get_cythonized_result(self, how, grouper, aggregate=False,
20011995
return self._wrap_transformed_output(output)
20021996

20031997
@Substitution(name='groupby')
2004-
@Appender(_doc_template)
1998+
@Appender(_common_see_also)
20051999
def shift(self, periods=1, freq=None, axis=0):
20062000
"""
20072001
Shift each group by periods observations.
@@ -2024,7 +2018,7 @@ def shift(self, periods=1, freq=None, axis=0):
20242018
periods=periods)
20252019

20262020
@Substitution(name='groupby')
2027-
@Appender(_doc_template)
2021+
@Appender(_common_see_also)
20282022
def pct_change(self, periods=1, fill_method='pad', limit=None, freq=None,
20292023
axis=0):
20302024
"""
@@ -2041,19 +2035,16 @@ def pct_change(self, periods=1, fill_method='pad', limit=None, freq=None,
20412035
shifted = fill_grp.shift(periods=periods, freq=freq)
20422036
return (filled / shifted) - 1
20432037

2044-
@Substitution(name='groupby')
2038+
@Substitution(name='groupby',
2039+
see_also=_common_see_also)
20452040
def head(self, n=5):
20462041
"""
20472042
Returns first n rows of each group.
20482043
20492044
Essentially equivalent to ``.apply(lambda x: x.head(n))``,
20502045
except ignores as_index flag.
20512046
2052-
See Also
2053-
--------
2054-
pandas.Series.%(name)s
2055-
pandas.DataFrame.%(name)s
2056-
pandas.Panel.%(name)s
2047+
%(see_also)s
20572048
20582049
Examples
20592050
--------
@@ -2073,19 +2064,16 @@ def head(self, n=5):
20732064
mask = self._cumcount_array() < n
20742065
return self._selected_obj[mask]
20752066

2076-
@Substitution(name='groupby')
2067+
@Substitution(name='groupby',
2068+
see_also=_common_see_also)
20772069
def tail(self, n=5):
20782070
"""
20792071
Returns last n rows of each group.
20802072
20812073
Essentially equivalent to ``.apply(lambda x: x.tail(n))``,
20822074
except ignores as_index flag.
20832075
2084-
See Also
2085-
--------
2086-
pandas.Series.%(name)s
2087-
pandas.DataFrame.%(name)s
2088-
pandas.Panel.%(name)s
2076+
%(see_also)s
20892077
20902078
Examples
20912079
--------

0 commit comments

Comments
 (0)