NaN means Not-a-Quantity. You should utilize it in numerical libraries — but in addition within the Python normal library.
NaN stands for Not-a-Quantity. Thus, a NaN object represents what this very title conveys — one thing that isn’t a quantity. It may be a lacking worth but in addition a non-numerical worth in a numerical variable. As we shouldn’t use a non-numerical worth in purely numerical containers, we point out such a price as not-a-number, NaN. In different phrases, we will say NaN represents a lacking numerical worth.
On this article, we’ll talk about NaN objects accessible within the Python normal library.
NaN values happen regularly in numerical information. When you’re all in favour of particulars of this worth, you can find them, as an example, right here:
On this article, we is not going to talk about all the main points of NaN values.¹ As an alternative, we’ll talk about a number of examples of how one can work with NaN values in Python.
Every programming language has its personal method to NaN values. In programming languages targeted on computation, NaN values are basic. For instance, in R, you’ve NULL (a counterpart of Python’s None), NA (for not accessible), and NaN (for not-a-number):
Screenshot from an R session. Picture by creator.
In Python, you’ve None and plenty of objects representing NaN. It’s price to know that Pandas differentiates between NaN and NaT, a price representing lacking time. This text will talk about NaN values in the usual library; NaN (and NaT, for that matter) within the mainstream numerical Python frameworks — comparable to NumPy and Pandas — will likely be coated in a future article.
When you haven’t labored with numerical information in Python, it’s possible you’ll not have encountered NaN in any respect. Nonetheless, NaN values are ubiquitous in Python programming, so it’s essential to…