Examples

In [1]:
import numpy as np
import pandas as pd
In [2]:
idx = pd.MultiIndex.from_arrays([
    ['warm', 'warm', 'cold', 'cold'],
    ['fox', 'cat', 'snake', 'spider']],
    names=['blooded', 'animal'])
In [3]:
s = pd.Series([4, 4, 0, 8], name='legs', index=idx)
s
Out[3]:
blooded  animal
warm     fox       4
         cat       4
cold     snake     0
         spider    8
Name: legs, dtype: int64

In [4]:
s.sum()
Out[4]:
16

Sum using level names, as well as indices.

In [5]:
s.sum(level='blooded')
Out[5]:
blooded
warm    8
cold    8
Name: legs, dtype: int64

By default, the sum of an empty or all-NA Series is 0.

In [6]:
pd.Series([]).sum()  # min_count=0 is the default
Out[6]:
0.0

This can be controlled with the min_count parameter. For example, if you’d like the sum of an empty
series to be NaN, pass min_count=1.

In [7]:
pd.Series([]).sum(min_count=1)
Out[7]:
nan

Thanks to the skipna parameter, min_count handles all-NA and empty series identically.

In [8]:
pd.Series([np.nan]).sum()
Out[8]:
0.0
In [9]:
pd.Series([np.nan]).sum(min_count=1)
Out[9]:
nan