Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python - check if nan float in dictionary

import numpy as np
import pandas as pd
import tia.bbg.datamgr as dm
mgr = dm.BbgDataManager()

bb_yearb4 = "2016-12-30"
bb_today = "2017-09-22"

indices = [list of indices]
sids_index = mgr[indices]
df_idx = sids_index.get_historical('PX_LAST', bb_yearb4, bb_today)

nan = np.nan

price_test = {}
for index in indices:
    price_test["{0}".format(index)] = df_idx.loc[bb_today][index]

The output shows multiple nan float values:

In [1]: price_test.values()
Out[1]: [nan, nan, nan, 47913.199999999997, nan, 1210.3299999999999, nan]

However, testing for nan shows false:

In [2]: nan in price_test.values()
Out[2]: False

What is the correct way to test this?

like image 940
theurlin Avatar asked Sep 22 '17 16:09

theurlin


People also ask

How do I know if my NaN is float?

To check whether a floating point or double number is NaN (Not a Number) in C++, we can use the isnan() function. The isnan() function is present into the cmath library. This function is introduced in C++ version 11.

Is NaN a float pandas?

nan . In Working with missing data, we saw that pandas primarily uses NaN to represent missing data. Because NaN is a float, this forces an array of integers with any missing values to become floating point.

How do you know if something is a NaN?

isNaN() Method: To determine whether a number is NaN, we can use the isNaN() function. It is a boolean function that returns true if a number is NaN otherwise returns false.


1 Answers

NaN is weird, because NaN != NaN. There's a good reason for that, but it still breaks in checks and everything else that assumes normal == behavior.

Check for NaN with NaN-specific checks, like numpy.isnan:

any(np.isnan(val) for val in d.values())

or in a non-NumPy context,

any(math.isnan(val) for val in d.values())
like image 87
user2357112 supports Monica Avatar answered Sep 24 '22 00:09

user2357112 supports Monica