Questions tagged [nan]

NaN is an abbreviation for "Not a Number". NaN is sometimes not equal to itself.

NaN (Not a Number) is a value of the numeric data type representing an undefined or unrepresentable value, especially in floating-point calculations. Systematic use of NaNs was introduced by the IEEE 754 floating-point standard in 1985, along with the representation of other non-finite quantities such as infinity.

A common feature of NaN in many programming languages is that NaN is not equal to itself.

4290 questions
280
votes
7 answers

pandas GroupBy columns with NaN (missing) values

I have a DataFrame with many missing values in columns which I wish to groupby: import pandas as pd import numpy as np df = pd.DataFrame({'a': ['1', '2', '3'], 'b': ['4', np.NaN, '6']}) In [4]: df.groupby('b').groups Out[4]: {'4': [0], '6':…
Gyula Sámuel Karli
  • 3,118
  • 2
  • 15
  • 18
266
votes
14 answers

How to find which columns contain any NaN value in Pandas dataframe

Given a pandas dataframe containing possible NaN values scattered here and there: Question: How do I determine which columns contain NaN values? In particular, can I get a list of the column names containing NaNs?
Jesper - jtk.eth
  • 7,026
  • 11
  • 36
  • 63
258
votes
10 answers

How to replace NaNs by preceding or next values in pandas DataFrame?

Suppose I have a DataFrame with some NaNs: >>> import pandas as pd >>> df = pd.DataFrame([[1, 2, 3], [4, None, None], [None, None, 9]]) >>> df 0 1 2 0 1 2 3 1 4 NaN NaN 2 NaN NaN 9 What I need to do is replace every NaN with the…
zegkljan
  • 8,051
  • 5
  • 34
  • 49
240
votes
5 answers

Is it possible to set a number to NaN or infinity?

Is it possible to set an element of an array to NaN in Python? Additionally, is it possible to set a variable to +/- infinity? If so, is there any function to check whether a number is infinity or not?
Bob
  • 10,741
  • 27
  • 89
  • 143
222
votes
22 answers

Why does typeof NaN return 'number'?

Just out of curiosity. It doesn't seem very logical that typeof NaN is number. Just like NaN === NaN or NaN == NaN returning false, by the way. Is this one of the peculiarities of JavaScript, or would there be a reason for this? Edit: thanks for…
KooiInc
  • 119,216
  • 31
  • 141
  • 177
191
votes
25 answers

Why does isNaN(" ") (string with spaces) equal false?

In JavaScript, why does isNaN(" ") evaluate to false, but isNaN(" x") evaluate to true? I’m performing numerical operations on a text input field, and I’m checking if the field is null, "", or NaN. When someone types a handful of spaces into the…
IVR Avenger
  • 15,090
  • 13
  • 46
  • 57
189
votes
7 answers

Python Pandas replace NaN in one column with value from corresponding row of second column

I am working with this Pandas DataFrame in Python. File heat Farheit Temp_Rating 1 YesQ 75 N/A 1 NoR 115 N/A 1 YesA 63 N/A 1 NoT 83 41 1 NoY …
edesz
  • 11,756
  • 22
  • 75
  • 123
176
votes
7 answers

Replace None with NaN in pandas dataframe

I have table x: website 0 http://www.google.com/ 1 http://www.yahoo.com 2 None I want to replace python None with pandas NaN. I tried: x.replace(to_replace=None, value=np.nan) But I got: TypeError: 'regex' must be a string or a…
AdamNYC
  • 19,887
  • 29
  • 98
  • 154
165
votes
10 answers

How to set a cell to NaN in a pandas dataframe

I'd like to replace bad values in a column of a dataframe by NaN's. mydata = {'x' : [10, 50, 18, 32, 47, 20], 'y' : ['12', '11', 'N/A', '13', '15', 'N/A']} df = pd.DataFrame(mydata) df[df.y == 'N/A']['y'] = np.nan Though, the last line fails and…
Mark Morrisson
  • 2,543
  • 4
  • 19
  • 25
163
votes
6 answers

Why is NaN not equal to NaN?

The relevant IEEE standard defines a numeric constant NaN (not a number) and prescribes that NaN should compare as not equal to itself. Why is that? All the languages I'm familiar with implement this rule. But it often causes significant problems,…
max
  • 49,282
  • 56
  • 208
  • 355
162
votes
10 answers

Why does Double.NaN==Double.NaN return false?

I was just studying OCPJP questions and I found this strange code: public static void main(String a[]) { System.out.println(Double.NaN==Double.NaN); System.out.println(Double.NaN!=Double.NaN); } When I ran the code, I got: false true How…
Maverick
  • 3,053
  • 6
  • 24
  • 30
160
votes
8 answers

Fast check for NaN in NumPy

I'm looking for the fastest way to check for the occurrence of NaN (np.nan) in a NumPy array X. np.isnan(X) is out of the question, since it builds a boolean array of shape X.shape, which is potentially gigantic. I tried np.nan in X, but that seems…
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
154
votes
5 answers

C/C++ NaN constant (literal)?

Is this possible to assign a NaN to a double or float in C/C++? Like in JavaScript you do: a = NaN. So later you can check if the variable is a number or no.
exebook
  • 32,014
  • 33
  • 141
  • 226
152
votes
2 answers

What is the difference between quiet NaN and signaling NaN?

I have read about floating-point and I understand that NaN could result from operations. But I can't understand what these are concepts exactly. What is the difference between them? Which one can be produced during C++ programming? As a programmer,…
JalalJaberi
  • 2,417
  • 8
  • 25
  • 41
151
votes
5 answers

What is the difference between (NaN != NaN) and (NaN !== NaN)?

First of all I want to mention that I know how isNaN() and Number.isNaN() work. I am reading The Definite Guide by David Flanagan and he gives an example for how to check if the value is NaN: x !== x This will result in true if and only if x is…
Giorgi Nakeuri
  • 35,155
  • 8
  • 47
  • 75