gpt4 book ai didi

python-2.7 - python - 检查字典中的nan是否 float

转载 作者:行者123 更新时间:2023-12-04 16:58:02 27 4
gpt4 key购买 nike

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]

输出显示多个 nan 浮点值:
In [1]: price_test.values()
Out[1]: [nan, nan, nan, 47913.199999999997, nan, 1210.3299999999999, nan]

但是,对 nan 的测试显示错误:
In [2]: nan in price_test.values()
Out[2]: False

测试这个的正确方法是什么?

最佳答案

NaN 很奇怪,因为 NaN != NaN。这是有充分理由的,但它仍然会中断 in检查和其他一切假设正常 ==行为。

使用特定于 NaN 的检查检查 NaN,例如 numpy.isnan :

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

或在非 NumPy 上下文中,
any(math.isnan(val) for val in d.values())

关于python-2.7 - python - 检查字典中的nan是否 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46369544/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com