O'Reilly logo

Learning pandas by Michael Heydt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The special case of Not-A-Number (NaN)

pandas mathematical operators and functions handle NaN in a special manner (compared to NumPy) that does not break the computations. pandas is lenient with missing data assuming that it is a common situation.

To demonstrate the difference, we can examine the following code, which calculates the mean of a NumPy array:

In [54]:
   # mean of numpy array values
   nda = np.array([1, 2, 3, 4, 5])
   nda.mean()

Out[54]:
   3.0

The result is as expected. The following code replaces one value with a NaN value:

In [55]:
   # mean of numpy array values with a NaN
   nda = np.array([1, 2, 3, 4, np.NaN])
   nda.mean()

Out[55]:
   nan

When encountering a NaN value, NumPy simply returns NaN. pandas changes this, so that NaN values are ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required