Search⌘ K
AI Features

The sum() Method with Pandas Series

Explore how the sum method operates on pandas Series, especially when dealing with NaN values and data type conversions. Understand pandas' approach to missing data, the behavior of integer and float types, and how to avoid common bugs in your calculations.

We'll cover the following...

Try it yourself

Try executing the code below to see the result.

Python 3.5
import pandas as pd
v1 = pd.Series([0, 2, 4])
v2 = pd.Series([0, 1, 2])
out = v1 // v2
print(out.sum())

Explanation

There are a few things going on in this teaser. The first has to do with the // operator in out = v1 // v2. This is the floordivReturns an integral part of the quotient. operator in Python. Unlike the regular division, it returns an integer.

In [1]: 7/2
Out[1]: 3.5
In [2]: 7//2
Out[2]: 3

The // operator is useful when we want to calculate indices (for example, in a binary search).

The next odd thing is that we managed to divide by 0 ...