nat = np.datetime64('NaT')
nat == nat
>> FutureWarning: In the future, 'NAT == x' and 'x == NAT' will always be False.
np.isnan(nat)
>> 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''
如何检查datetime64是否为NaT?我好像从文档里找不出什么东西来。我知道Pandas可以做到这一点,但我不想为这么简单的东西添加依赖。
发布于 2017-05-08 04:15:24
从NumPy版本1.13开始,它包含一个isnat
函数:
>>> import numpy as np
>>> np.isnat(np.datetime64('nat'))
True
它也适用于数组:
>>> np.isnat(np.array(['nat', 1, 2, 3, 4, 'nat', 5], dtype='datetime64[D]'))
array([ True, False, False, False, False, True, False], dtype=bool)
发布于 2017-02-08 09:11:44
这种方法避免了警告,同时保留了面向数组的计算。
import numpy as np
def isnat(x):
"""
datetime64 analog to isnan.
doesn't yet exist in numpy - other ways give warnings
and are likely to change.
"""
return x.astype('i8') == np.datetime64('NaT').astype('i8')
发布于 2016-10-19 19:43:17
另一种方法是捕获exeption:
def is_nat(npdatetime):
try:
npdatetime.strftime('%x')
return False
except:
return True
https://stackoverflow.com/questions/38509538
复制相似问题