Skip to content

Commit 159772d

Browse files
vadakattuWillAyd
authored andcommitted
DOC: Added MultiIndex Example for Series Min (#23338)
1 parent 6b31abd commit 159772d

File tree

1 file changed

+96
-94
lines changed

1 file changed

+96
-94
lines changed

pandas/core/generic.py

+96-94
Original file line numberDiff line numberDiff line change
@@ -9905,16 +9905,16 @@ def _add_numeric_operations(cls):
99059905

99069906
cls.any = _make_logical_function(
99079907
cls, 'any', name, name2, axis_descr, _any_desc, nanops.nanany,
9908-
_any_examples, _any_see_also, empty_value=False)
9908+
_any_see_also, _any_examples, empty_value=False)
99099909
cls.all = _make_logical_function(
99109910
cls, 'all', name, name2, axis_descr, _all_desc, nanops.nanall,
9911-
_all_examples, _all_see_also, empty_value=True)
9911+
_all_see_also, _all_examples, empty_value=True)
99129912

99139913
@Substitution(outname='mad',
99149914
desc="Return the mean absolute deviation of the values "
99159915
"for the requested axis.",
99169916
name1=name, name2=name2, axis_descr=axis_descr,
9917-
min_count='', examples='')
9917+
min_count='', see_also='', examples='')
99189918
@Appender(_num_doc)
99199919
def mad(self, axis=None, skipna=None, level=None):
99209920
if skipna is None:
@@ -9956,7 +9956,7 @@ def mad(self, axis=None, skipna=None, level=None):
99569956
desc="Return the compound percentage of the values for "
99579957
"the requested axis.", name1=name, name2=name2,
99589958
axis_descr=axis_descr,
9959-
min_count='', examples='')
9959+
min_count='', see_also='', examples='')
99609960
@Appender(_num_doc)
99619961
def compound(self, axis=None, skipna=None, level=None):
99629962
if skipna is None:
@@ -9984,8 +9984,9 @@ def compound(self, axis=None, skipna=None, level=None):
99849984

99859985
cls.sum = _make_min_count_stat_function(
99869986
cls, 'sum', name, name2, axis_descr,
9987-
'Return the sum of the values for the requested axis.',
9988-
nanops.nansum, _sum_examples)
9987+
"""Return the sum of the values for the requested axis.\n
9988+
This is equivalent to the method ``numpy.sum``.""",
9989+
nanops.nansum, _stat_func_see_also, _sum_examples)
99899990
cls.mean = _make_stat_function(
99909991
cls, 'mean', name, name2, axis_descr,
99919992
'Return the mean of the values for the requested axis.',
@@ -10004,24 +10005,24 @@ def compound(self, axis=None, skipna=None, level=None):
1000410005
cls.prod = _make_min_count_stat_function(
1000510006
cls, 'prod', name, name2, axis_descr,
1000610007
'Return the product of the values for the requested axis.',
10007-
nanops.nanprod, _prod_examples)
10008+
nanops.nanprod, examples=_prod_examples)
1000810009
cls.product = cls.prod
1000910010
cls.median = _make_stat_function(
1001010011
cls, 'median', name, name2, axis_descr,
1001110012
'Return the median of the values for the requested axis.',
1001210013
nanops.nanmedian)
1001310014
cls.max = _make_stat_function(
1001410015
cls, 'max', name, name2, axis_descr,
10015-
"""This method returns the maximum of the values in the object.
10016+
"""Return the maximum of the values for the requested axis.\n
1001610017
If you want the *index* of the maximum, use ``idxmax``. This is
1001710018
the equivalent of the ``numpy.ndarray`` method ``argmax``.""",
10018-
nanops.nanmax, _max_examples)
10019+
nanops.nanmax, _stat_func_see_also, _max_examples)
1001910020
cls.min = _make_stat_function(
1002010021
cls, 'min', name, name2, axis_descr,
10021-
"""This method returns the minimum of the values in the object.
10022+
"""Return the minimum of the values for the requested axis.\n
1002210023
If you want the *index* of the minimum, use ``idxmin``. This is
1002310024
the equivalent of the ``numpy.ndarray`` method ``argmin``.""",
10024-
nanops.nanmin)
10025+
nanops.nanmin, _stat_func_see_also, _min_examples)
1002510026

1002610027
@classmethod
1002710028
def _add_series_only_operations(cls):
@@ -10174,21 +10175,25 @@ def _doc_parms(cls):
1017410175
Parameters
1017510176
----------
1017610177
axis : %(axis_descr)s
10177-
skipna : boolean, default True
10178+
Axis for the function to be applied on.
10179+
skipna : bool, default True
1017810180
Exclude NA/null values when computing the result.
1017910181
level : int or level name, default None
1018010182
If the axis is a MultiIndex (hierarchical), count along a
10181-
particular level, collapsing into a %(name1)s
10182-
numeric_only : boolean, default None
10183+
particular level, collapsing into a %(name1)s.
10184+
numeric_only : bool, default None
1018310185
Include only float, int, boolean columns. If None, will attempt to use
1018410186
everything, then use only numeric data. Not implemented for Series.
1018510187
%(min_count)s\
10188+
**kwargs
10189+
Additional keyword arguments to be passed to the function.
1018610190
1018710191
Returns
1018810192
-------
10189-
%(outname)s : %(name1)s or %(name2)s (if level specified)\
10190-
10191-
%(examples)s"""
10193+
%(outname)s : %(name1)s or %(name2)s (if level specified)
10194+
%(see_also)s
10195+
%(examples)s\
10196+
"""
1019210197

1019310198
_num_ddof_doc = """
1019410199
%(desc)s
@@ -10692,43 +10697,49 @@ def _doc_parms(cls):
1069210697
Series([], dtype: bool)
1069310698
"""
1069410699

10695-
_sum_examples = """\
10700+
_shared_docs['stat_func_example'] = """\
1069610701
Examples
1069710702
--------
10698-
``MultiIndex`` series example of monthly rainfall
1069910703
10700-
>>> index = pd.MultiIndex.from_product(
10701-
... [['London', 'New York'], ['Jun', 'Jul', 'Aug']],
10702-
... names=['city', 'month'])
10703-
>>> s = pd.Series([47, 35, 54, 112, 117, 113], index=index)
10704+
>>> idx = pd.MultiIndex.from_arrays([
10705+
... ['warm', 'warm', 'cold', 'cold'],
10706+
... ['dog', 'falcon', 'fish', 'spider']],
10707+
... names=['blooded', 'animal'])
10708+
>>> s = pd.Series([4, 2, 0, 8], name='legs', index=idx)
1070410709
>>> s
10705-
city month
10706-
London Jun 47
10707-
Jul 35
10708-
Aug 54
10709-
New York Jun 112
10710-
Jul 117
10711-
Aug 113
10712-
dtype: int64
10713-
10714-
>>> s.sum()
10715-
478
10716-
10717-
Sum using level names, as well as indices
10718-
10719-
>>> s.sum(level='city')
10720-
city
10721-
London 136
10722-
New York 342
10723-
dtype: int64
10724-
10725-
>>> s.sum(level=1)
10726-
month
10727-
Jun 159
10728-
Jul 152
10729-
Aug 167
10730-
dtype: int64
10710+
blooded animal
10711+
warm dog 4
10712+
falcon 2
10713+
cold fish 0
10714+
spider 8
10715+
Name: legs, dtype: int64
10716+
10717+
>>> s.{stat_func}()
10718+
{default_output}
10719+
10720+
{verb} using level names, as well as indices.
10721+
10722+
>>> s.{stat_func}(level='blooded')
10723+
blooded
10724+
warm {level_output_0}
10725+
cold {level_output_1}
10726+
Name: legs, dtype: int64
10727+
10728+
>>> s.{stat_func}(level=0)
10729+
blooded
10730+
warm {level_output_0}
10731+
cold {level_output_1}
10732+
Name: legs, dtype: int64
10733+
"""
10734+
10735+
_sum_examples = _shared_docs['stat_func_example'].format(
10736+
stat_func='sum',
10737+
verb='Sum',
10738+
default_output=14,
10739+
level_output_0=6,
10740+
level_output_1=8)
1073110741

10742+
_sum_examples += """
1073210743
By default, the sum of an empty or all-NA Series is ``0``.
1073310744
1073410745
>>> pd.Series([]).sum() # min_count=0 is the default
@@ -10750,6 +10761,35 @@ def _doc_parms(cls):
1075010761
nan
1075110762
"""
1075210763

10764+
_max_examples = _shared_docs['stat_func_example'].format(
10765+
stat_func='max',
10766+
verb='Max',
10767+
default_output=8,
10768+
level_output_0=4,
10769+
level_output_1=8)
10770+
10771+
_min_examples = _shared_docs['stat_func_example'].format(
10772+
stat_func='min',
10773+
verb='Min',
10774+
default_output=0,
10775+
level_output_0=2,
10776+
level_output_1=0)
10777+
10778+
_stat_func_see_also = """
10779+
See Also
10780+
--------
10781+
Series.sum : Return the sum.
10782+
Series.min : Return the minimum.
10783+
Series.max : Return the maximum.
10784+
Series.idxmin : Return the index of the minimum.
10785+
Series.idxmax : Return the index of the maximum.
10786+
DataFrame.min : Return the sum over the requested axis.
10787+
DataFrame.min : Return the minimum over the requested axis.
10788+
DataFrame.max : Return the maximum over the requested axis.
10789+
DataFrame.idxmin : Return the index of the minimum over the requested axis.
10790+
DataFrame.idxmax : Return the index of the maximum over the requested axis.
10791+
"""
10792+
1075310793
_prod_examples = """\
1075410794
Examples
1075510795
--------
@@ -10773,45 +10813,6 @@ def _doc_parms(cls):
1077310813
nan
1077410814
"""
1077510815

10776-
_max_examples = """\
10777-
Examples
10778-
--------
10779-
``MultiIndex`` series example of monthly rainfall
10780-
10781-
>>> index = pd.MultiIndex.from_product(
10782-
... [['London', 'New York'], ['Jun', 'Jul', 'Aug']],
10783-
... names=['city', 'month'])
10784-
>>> s = pd.Series([47, 35, 54, 112, 117, 113], index=index)
10785-
>>> s
10786-
city month
10787-
London Jun 47
10788-
Jul 35
10789-
Aug 54
10790-
New York Jun 112
10791-
Jul 117
10792-
Aug 113
10793-
dtype: int64
10794-
10795-
>>> s.max()
10796-
117
10797-
10798-
Max using level names, as well as indices
10799-
10800-
>>> s.max(level='city')
10801-
city
10802-
London 54
10803-
New York 117
10804-
dtype: int64
10805-
10806-
>>> s.max(level=1)
10807-
month
10808-
Jun 112
10809-
Jul 117
10810-
Aug 113
10811-
dtype: int64
10812-
"""
10813-
10814-
1081510816
_min_count_stub = """\
1081610817
min_count : int, default 0
1081710818
The required number of valid values to perform the operation. If fewer than
@@ -10826,10 +10827,10 @@ def _doc_parms(cls):
1082610827

1082710828

1082810829
def _make_min_count_stat_function(cls, name, name1, name2, axis_descr, desc,
10829-
f, examples):
10830+
f, see_also='', examples=''):
1083010831
@Substitution(outname=name, desc=desc, name1=name1, name2=name2,
1083110832
axis_descr=axis_descr, min_count=_min_count_stub,
10832-
examples=examples)
10833+
see_also=see_also, examples=examples)
1083310834
@Appender(_num_doc)
1083410835
def stat_func(self, axis=None, skipna=None, level=None, numeric_only=None,
1083510836
min_count=0,
@@ -10854,9 +10855,10 @@ def stat_func(self, axis=None, skipna=None, level=None, numeric_only=None,
1085410855

1085510856

1085610857
def _make_stat_function(cls, name, name1, name2, axis_descr, desc, f,
10857-
examples=''):
10858+
see_also='', examples=''):
1085810859
@Substitution(outname=name, desc=desc, name1=name1, name2=name2,
10859-
axis_descr=axis_descr, min_count='', examples=examples)
10860+
axis_descr=axis_descr, min_count='', see_also=see_also,
10861+
examples=examples)
1086010862
@Appender(_num_doc)
1086110863
def stat_func(self, axis=None, skipna=None, level=None, numeric_only=None,
1086210864
**kwargs):
@@ -10933,9 +10935,9 @@ def cum_func(self, axis=None, skipna=True, *args, **kwargs):
1093310935

1093410936

1093510937
def _make_logical_function(cls, name, name1, name2, axis_descr, desc, f,
10936-
examples, see_also, empty_value):
10938+
see_also, examples, empty_value):
1093710939
@Substitution(outname=name, desc=desc, name1=name1, name2=name2,
10938-
axis_descr=axis_descr, examples=examples, see_also=see_also,
10940+
axis_descr=axis_descr, see_also=see_also, examples=examples,
1093910941
empty_value=empty_value)
1094010942
@Appender(_bool_doc)
1094110943
def logical_func(self, axis=0, bool_only=None, skipna=True, level=None,

0 commit comments

Comments
 (0)