The all() Method with Pandas Series
Let's find out how the all() and any() methods work with pandas series.
We'll cover the following
Try it yourself
Try executing the code below to see the result.
import pandas as pds = pd.Series([], dtype='float64')print('full' if s.all() else 'empty')
Explanation
The pandas.Series.all
documentation says the following:
It returns whether all elements are
True
, potentially over an axis.It returns
True
unless there is at least one element within a series or along a DataFrame axis that isFalse
or something that is equivalent (for example, zero or empty).
The last bullet point explains what we see. There aren’t any False
elements in the empty series. The built-in all
function behaves the same way, like this:
In [1]: all([])
Out[1]: True
The all
function is like the mathematical ∀ symbol, “By convention, the formula ∀x∈∅,P(x) is always true, regardless of the formula
P(x) …”
The any
function has the same logic, only reversed. It implies at least one element exists that is always False
, which we can see in the sequence below:
In [1]: import pandas as pd
In [2]: import numpy as np
In [3]: any([])
Out[3]: False
In [5]: pd.Series([], dtype=np.float64).any()
Out[5]: False
Get hands-on with 1300+ tech skills courses.