Skip to content

Commit 8f3066e

Browse files
vadakattutm9k1
authored andcommitted
DOC: Added a Multi Index example for the Series.sum method (pandas-dev#23279)
1 parent 6d220c7 commit 8f3066e

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

pandas/core/generic.py

+34
Original file line numberDiff line numberDiff line change
@@ -10185,6 +10185,40 @@ def _doc_parms(cls):
1018510185
_sum_examples = """\
1018610186
Examples
1018710187
--------
10188+
``MultiIndex`` series example of monthly rainfall
10189+
10190+
>>> index = pd.MultiIndex.from_product(
10191+
... [['London', 'New York'], ['Jun', 'Jul', 'Aug']],
10192+
... names=['city', 'month'])
10193+
>>> s = pd.Series([47, 35, 54, 112, 117, 113], index=index)
10194+
>>> s
10195+
city month
10196+
London Jun 47
10197+
Jul 35
10198+
Aug 54
10199+
New York Jun 112
10200+
Jul 117
10201+
Aug 113
10202+
dtype: int64
10203+
10204+
>>> s.sum()
10205+
478
10206+
10207+
Sum using level names, as well as indices
10208+
10209+
>>> s.sum(level='city')
10210+
city
10211+
London 136
10212+
New York 342
10213+
dtype: int64
10214+
10215+
>>> s.sum(level=1)
10216+
month
10217+
Jun 159
10218+
Jul 152
10219+
Aug 167
10220+
dtype: int64
10221+
1018810222
By default, the sum of an empty or all-NA Series is ``0``.
1018910223
1019010224
>>> pd.Series([]).sum() # min_count=0 is the default

0 commit comments

Comments
 (0)