TypeError: ufunc 'isnan' not supported ... when trying to remove nan float type from dictionary value

TypeError: ufunc 'isnan' not supported ... when trying to remove nan float type from dictionary value

d = {'E1': ['C',
        'Me',
        'Lans',
        nan,
        '3050',
        '55901',
        nan,
        nan,
        '2011-09-05 00:00:00',
        '3050-09-02 00:00:00',
        '2011-09-05 00:00:00',
        '3050-09-02 00:00:00'],
 'E2': ['Can',
        'Mar',
        'Horns',
        '26D',
        '1001',
        '14086',
        nan,
        '(100) 300-2345',
        '1001-09-02 00:00:00',
        '0100-09-02 00:00:00',
        '2011-19-26 00:00:00',
        '1001-09-02 00:00:00',
        '0100-09-02 00:00:00',
        '2011-19-26 00:00:00']}

我有以下字典 d,我想去掉 nan。根据 我可以使用以下方法

for k,v in d.items():
    if np.isnan(v[1]):
        d.pop(k)
print(d)

但是我得到一个错误

TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

所以我查看了 ,它建议使用

pd.isnull 而不是 np.isna 但这也不起作用。所以我想知道,如何摆脱 nan?

尝试使用 isinstance

例如:

d = {k: [i for i in v if isinstance(i, str)] for k,v in d.items()}
print(d)

输出:

{'E1': ['C',
        'Me',
        'Lans',
        '3050',
        '55901',
        '2011-09-05 00:00:00',
        '3050-09-02 00:00:00',
        '2011-09-05 00:00:00',
        '3050-09-02 00:00:00'],
 'E2': ['Can',
        'Mar',
        'Horns',
        '26D',
        '1001',
        '14086',
        '(100) 300-2345',
        '1001-09-02 00:00:00',
        '0100-09-02 00:00:00',
        '2011-19-26 00:00:00',
        '1001-09-02 00:00:00',
        '0100-09-02 00:00:00',
        '2011-19-26 00:00:00']}