Sequential way to check if np.array is datetime-like

I am testing part of unit testing, and I need to make sure that the function always returns an object of type np.datetime64. However, they can be any unit (year, day, nanosecond, etc.).

I tried:

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)

Any suggestions?

+4
source share
2 answers

I am currently using:

assert 'datetime64' in str(comp.dtype)

It leaves a bad taste in the mouth (as it relies on a different behavior), but all I can do with it.

+2
source

You can use issubdtype:

np.issubdtype(comp.dtype, np.datetime64)
+6
source

Source: https://habr.com/ru/post/1536463/


All Articles