Logical Operators with Pandas Series

Let's find out how pandas series works with NaN.

We'll cover the following

Try it yourself

Try executing the code below to see the result.

Press + to interact
import numpy as np
import pandas as pd
s = pd.Series([1, np.nan, 3])
print(s[~(s == np.nan)])

Explanation

We covered some floating-point oddities in the “Multiplying” puzzle. The NaN (or np.nan) is another oddity. The name NaN means not a number. In programming, NaN is used in these two scenarios:

  1. When the code calls for computation that cannot be completed.
  2. If values are missing.

Here’s an example of a computation that cannot be completed:

In [1]: np.float64(0)/np.float64(0)
RuntimeWarning: invalid value encountered in \
double_scalars np.float64(0)/np.float64(0)
Out[1]: nan

We see a warning but not an exception. The return value is nan, which doesn’t equal any number, including itself.

In [2]: np.nan == np.nan
Out[2]: False

We can use pandas.isnull to fix this teaser. To check that a value is nan, we need to use a special function such as pandas.isnull, like this:

In [3]: pd.isnull(np.nan)
Out[3]: True

Solution

Press + to interact
import numpy as np
import pandas as pd
s = pd.Series([1, np.nan, 3])
print(s[~pd.isnull(s)])

The pandas.isnull function works with all pandas missing values such as None, pandas.NaT (not a time), and the new pandas.NA.

Floating points have several other special numbers such as inf (infinity), -inf, -0, +0, and others.

Get hands-on with 1300+ tech skills courses.