Handling of NaN values in mathematical operations

The NaN values are handled differently in pandas than in NumPy. We have seen this in earlier chapters, but it is worth revisiting here. This is demonstrated using the following example:

When a NumPy function encounters a NaN value, it returns NaN. Pandas functions typically ignore the NaN values and continue processing the function as though the NaN values were not part of the Series object.

Note that the mean of the preceding series was calculated as (1+2+3)/3 = 2, not (1+2+3)/4 or (1+2+0+4)/4. This verifies that NaN is totally ignored and not even counted as an item in Series.

More specifically, the way that Pandas handles the NaN values is as follows:

  • Summing of data treats NaN as 0
  • If all values are NaN, the result is NaN
  • Methods like .cumsum() and .cumprod() ignore the NaN values, but preserve them in the resulting arrays

The following demonstrates all these concepts:

But when using traditional mathematical operators, NaN will be propagated through to the result:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset