检查np.array是否类似于datetime的一致方法 [英] Consistent way to check if an np.array is datetime-like

查看:106
本文介绍了检查np.array是否类似于datetime的一致方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一些单元测试,我需要确保函数始终返回类似np.datetime64的对象.但是,它们可以是任何单位(年,日,纳秒等).

I'm dong some unit testing and I need to make sure a function always returns a np.datetime64-like object. However, they can be of any unit (year, day, nanosecond, etc).

我尝试过:

comp = function_returns_datetime_array(inp)

assert isinstance(comp.dtype, np.datetime64)
assert issubclass(comp.dtype, np.datetime64)
assert issubclass(type(comp.dtype), np.datetime64)

有什么建议吗?

推荐答案

您可以使用

You can use issubdtype:

np.issubdtype(comp.dtype, np.datetime64)

这篇关于检查np.array是否类似于datetime的一致方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆