C/C++ NaN constant (literal)?

In C, NAN is declared in <math.h>. In C++, std::numeric_limits<double>::quiet_NaN() is declared in <limits>. But for checking whether a value is NaN, you can’t compare it with another NaN value. Instead use isnan() from <math.h> in C, or std::isnan() from <cmath> in C++.

In JavaScript, why does zero divided by zero return NaN, but any other divided by zero return Infinity?

Because that’s how floating-point is defined (more generally than just Javascript). See for example: http://en.wikipedia.org/wiki/Floating-point#Infinities http://en.wikipedia.org/wiki/NaN#Creation Crudely speaking, you could think of 1/0 as the limit of 1/x as x tends to zero (from the right). And 0/0 has no reasonable interpretation at all, hence NaN.

NaNs as key in dictionaries

The problem here is that NaN is not equal to itself, as defined in the IEEE standard for floating point numbers: >>> float(“nan”) == float(“nan”) False When a dictionary looks up a key, it roughly does this: Compute the hash of the key to be looked up. For each key in the dict with the … Read more

Pandas DataFrames with NaNs equality comparison

You can use assert_frame_equals with check_names=False (so as not to check the index/columns names), which will raise if they are not equal: In [11]: from pandas.testing import assert_frame_equal In [12]: assert_frame_equal(df, expected, check_names=False) You can wrap this in a function with something like: try: assert_frame_equal(df, expected, check_names=False) return True except AssertionError: return False In more … Read more

convert nan value to zero

Where A is your 2D array: import numpy as np A[np.isnan(A)] = 0 The function isnan produces a bool array indicating where the NaN values are. A boolean array can by used to index an array of the same shape. Think of it like a mask.

Left justify string values in a pandas DataFrame

First, extract a slice of columns beginning with item – m = df.columns.str.contains(‘item’) i = df.iloc[:, m] Mask all values which meet your criteria. Use isin – j = i[~i.isin(df.makrc.tolist() + [‘not’])] Now. sort values based on NaNs and assign back – df.loc[:, m] = j.apply(sorted, key=pd.isnull, axis=1) df key sellyr brand makrc item1 item2 … Read more