NaN Values in the Python Standard Library | by Marcin Kozak | Oct, 2023


PYTHON PROGRAMMING

NaN means Not-a-Number. You can use it in numerical libraries — but also in the Python standard library.

Photo by cyrus gomez on Unsplash

NaN stands for Not-a-Number. Thus, a NaN object represents what this very name conveys — something that isn’t a number. It can be a missing value but also a non-numerical value in a numerical variable. As we shouldn’t use a non-numerical value in purely numerical containers, we indicate such a value as not-a-number, NaN. In other words, we can say NaN represents a missing numerical value.

In this article, we will discuss NaN objects available in the Python standard library.

NaN values occur frequently in numerical data. If you’re interested in details of this value, you will find them, for instance, here:

In this article, we will not discuss all the details of NaN values.¹ Instead, we will discuss several examples of how to work with NaN values in Python.

Each programming language has its own approach to NaN values. In programming languages focused on computation, NaN values are fundamental. For example, in R, you have NULL (a counterpart of Python’s None), NA (for not available), and NaN (for not-a-number):

R has NA for missing value and NaN for not-a-number, and NULL for None.
Screenshot from an R session. Image by author.

In Python, you have None and a number of objects representing NaN. It’s worth to know that Pandas differentiates between NaN and NaT, a value representing missing time. This article will discuss NaN values in the standard library; NaN (and NaT, for that matter) in the mainstream numerical Python frameworks — such as NumPy and Pandas — will be covered in a future article.

If you haven’t worked with numerical data in Python, you may not have encountered NaN at all. However, NaN values are ubiquitous in Python programming, so it’s important to…



Source link

Be the first to comment

Leave a Reply

Your email address will not be published.


*