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
1580
votes
19 answers

How to check for NaN values

float('nan') represents NaN (not a number). But how do I check for it?
Jack Ha
  • 19,661
  • 11
  • 37
  • 41
1402
votes
15 answers

How to drop rows of Pandas DataFrame whose value in a certain column is NaN

I have this DataFrame and want only the records whose EPS column is not NaN: >>> df STK_ID EPS cash STK_ID RPT_Date 601166 20111231 601166 NaN NaN 600036 20111231 600036 NaN 12 600016 20111231 600016 …
bigbug
  • 55,954
  • 42
  • 77
  • 96
772
votes
24 answers

Set value for particular cell in pandas DataFrame using index

I have created a Pandas DataFrame df = DataFrame(index=['A','B','C'], columns=['x','y']) and have got this x y A NaN NaN B NaN NaN C NaN NaN Now, I would like to assign a value to particular cell, for example to row C and column x. I…
Mitkp
  • 7,800
  • 3
  • 14
  • 8
693
votes
28 answers

How to check if any value is NaN in a Pandas DataFrame

In Python Pandas, what's the best way to check whether a DataFrame has one (or more) NaN values? I know about the function pd.isnan, but this returns a DataFrame of booleans for each element. This post right here doesn't exactly answer my question…
hlin117
  • 20,764
  • 31
  • 72
  • 93
630
votes
17 answers

How to replace NaN values by Zeroes in a column of a Pandas Dataframe?

I have a Pandas Dataframe as below: itm Date Amount 67 420 2012-09-30 00:00:00 65211 68 421 2012-09-09 00:00:00 29424 69 421 2012-09-16 00:00:00 29877 70 421 2012-09-23 00:00:00 30990 71 421 2012-09-30…
George Thompson
  • 6,627
  • 4
  • 16
  • 16
538
votes
33 answers

How do you check that a number is NaN in JavaScript?

I’ve only been trying it in Firefox’s JavaScript console, but neither of the following statements return true: parseFloat('geoff') == NaN; parseFloat('geoff') == Number.NaN;
Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
414
votes
21 answers

Checking if a double (or float) is NaN in C++

Is there an isnan() function? PS.: I'm in MinGW (if that makes a difference). I had this solved by using isnan() from , which doesn't exist in , which I was #includeing at first.
hasen
  • 161,647
  • 65
  • 194
  • 231
374
votes
8 answers

Pandas Replace NaN with blank/empty string

I have a Pandas Dataframe as shown below: 1 2 3 0 a NaN read 1 b l unread 2 c NaN read I want to remove the NaN values with an empty string so that it looks like so: 1 2 3 0 a "" read 1 b l …
user1452759
  • 8,810
  • 15
  • 42
  • 58
363
votes
29 answers

Convert Pandas column containing NaNs to dtype `int`

I read data from a .csv file to a Pandas dataframe as below. For one of the columns, namely id, I want to specify the column type as int. The problem is the id series has missing/empty values. When I try to cast the id column to integer while…
Zhubarb
  • 11,432
  • 18
  • 75
  • 114
360
votes
18 answers

How to turn NaN from parseInt into 0 for an empty string?

Is it possible somehow to return 0 instead of NaN when parsing values in JavaScript? In case of the empty string parseInt returns NaN. Is it possible to do something like that in JavaScript to check for NaN? var value = parseInt(tbb) == NaN ? 0 :…
Joper
  • 8,019
  • 21
  • 53
  • 80
354
votes
13 answers

How do I remove NaN values from a NumPy array?

How do I remove NaN values from a NumPy array? [1, 2, NaN, 4, NaN, 8] ⟶ [1, 2, 4, 8]
Dax Feliz
  • 12,220
  • 8
  • 30
  • 33
352
votes
12 answers

What is the rationale for all comparisons returning false for IEEE754 NaN values?

Why do comparisons of NaN values behave differently from all other values? That is, all comparisons with the operators ==, <=, >=, <, > where one or both values is NaN returns false, contrary to the behaviour of all other values. I suppose this…
starblue
  • 55,348
  • 14
  • 97
  • 151
327
votes
6 answers

How to select rows with one or more nulls from a pandas DataFrame without listing columns explicitly?

I have a dataframe with ~300K rows and ~40 columns. I want to find out if any rows contain null values - and put these 'null'-rows into a separate dataframe so that I could explore them easily. I can create a mask explicitly: mask = False for col in…
Lev Selector
  • 3,681
  • 3
  • 17
  • 8
315
votes
7 answers

How do you test to see if a double is equal to NaN?

I have a double in Java and I want to check if it is NaN. What is the best way to do this?
Eric Wilson
  • 57,719
  • 77
  • 200
  • 270
299
votes
12 answers

pandas DataFrame: replace nan values with average of columns

I've got a pandas DataFrame filled mostly with real numbers, but there is a few nan values in it as well. How can I replace the nans with averages of columns where they are? This question is very similar to this one: numpy array: replace nan values…
piokuc
  • 25,594
  • 11
  • 72
  • 102
1
2 3
99 100