Float.NaN == Float.NaN
Use Float.isNaN to check for NaN values.
Use Float.isNaN to check for NaN values.
Use the built in first_valid_index and last_valid_index they are designed specifically for this and slice your df: In [5]: first_idx = df.first_valid_index() last_idx = df.last_valid_index() print(first_idx, last_idx) df.loc[first_idx:last_idx] 1950 1954 Out[5]: sum 1950 5 1951 3 1952 NaN 1953 4 1954 8
Using floating point numbers, 0.0 / 0.0 isn’t a “divide by zero” error; it results in NaN. This C program prints -nan: #include <stdio.h> int main() { float x = 0.0 / 0.0; printf(“%f\n”, x); return 0; } In terms what NaN looks like to the computer, two “invalid” numbers are reserved for “signaling” and … Read more
The only difference is this: !isNan(1/0) // –> true isFinite(1/0) // –> false isNaN checks whether the argument is a number or not. The Infinities (+/-) are also numerical, thus they pass the isNaN check, but don’t pass the isFinite check. ** Note that any string which can be parsed as a number (“2”, “3.14”) … Read more
As I understand it, NaN is a sentinel instance of the Number class that represents, well, exactly what it stands for – numeric results that cannot be adequately represented. So 0/0 is not a number, in the sense that it’s NaN, but it is a Number in terms of its type. Perhaps it should have … Read more
You could perform a groupby/forward-fill operation on each group: import numpy as np import pandas as pd df = pd.DataFrame({‘id’: [1,1,2,2,1,2,1,1], ‘x’:[10,20,100,200,np.nan,np.nan,300,np.nan]}) df[‘x’] = df.groupby([‘id’])[‘x’].ffill() print(df) yields id x 0 1 10.0 1 1 20.0 2 2 100.0 3 2 200.0 4 1 20.0 5 2 200.0 6 1 300.0 7 1 300.0
From Wikipedia : In computing, 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 like infinities. And from MSDN : … Read more
As an alternate solution (this will linearly interpolate for arrays NaNs in the middle, as well): import numpy as np # Generate data… data = np.random.random(10) data[:2] = np.nan data[-1] = np.nan data[4:6] = np.nan print data # Fill in NaN’s… mask = np.isnan(data) data[mask] = np.interp(np.flatnonzero(mask), np.flatnonzero(~mask), data[~mask]) print data This yields: [ nan … Read more
It is a general rule how 64-bit floating point numbers are compared according to IEEE 754 (not Scala or even Java related, see NaN): double n1 = Double.NaN; double n2 = Double.NaN; System.out.println(n1 == n2); //false The idea is that NaN is a marker value for unknown or indeterminate. Comparing two unknown values should always … Read more