在Numpy中测试NaN和NaT的最佳方法实际上是使用Pandasisull()吗



使用我使用Pandasread_csv()导入的一个小csv数据集,丢失的一些值显示为Numpy NaN类型,而丢失的一些日期时间显示为Numpy NaT类型。这些列的实际数据类型不是这些类型,但Pandas并不知道——它认为它们就是这些类型。

为了测试这两种情况,似乎最好的方法实际上是使用Pandas的isnull()函数——我的问题是为什么Numpy没有内置这个功能?我是遗漏了什么,还是使用Pandas是测试Numpy类型的最佳方式?Numpy的内置isnan()函数似乎不是实现这一点的方法

更多上下文:

当使用Numpy的isnan()进行类型检查时,我收到了这个键入警告Module is not callable,并且深入挖掘它给我的错误是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''.,所以它似乎担心我传入了不同的类型。

要重新生成这个:将一个字符串传递给np.isnan(),它将抛出错误。例如CCD_ 10将抛出该错误。

为什么这不是使用这种方法的预期方式??传入不同的数据类型?为什么抛出错误是默认行为?如果数据类型实际上是Numpy数据类型,那么为什么使用Panda的isnull()来处理Numpy数据型检查似乎是最好的方法呢?

感谢所有

我的代码:

import pandas as pd
import numpy as np
from typing import Dict, Any
def a(my_obj:Dict[str, Any]):
for key, value in my_obj.items():
# this will throw an error if you pass in a string
if np.isnan(value):
my_obj[key] = None
# this one gives no error and actually works
if pd.isnull(value):
my_obj[key] = None
return my_obj

# this is true only if you comment out the `np.isnan()` lines
assert a({"working":"123", "typing_not_working":np.nan}) == {"working":"123", "typing_not_working":None}```
  • np.nan>gt>'浮点数据类型'
  • 此np.isnan不适用于"字符串类型"以及哪个测试元素

请检查以下内容:类型错误:ufunc';isnan';不支持输入类型,并且不能安全地强制输入

最新更新