Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Consistent way to check if an np.array is datetime-like

Tags:

datetime

numpy

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).

I've 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?

like image 819
JudoWill Avatar asked Apr 14 '14 14:04

JudoWill


2 Answers

You can use issubdtype:

np.issubdtype(comp.dtype, np.datetime64)
like image 156
IanS Avatar answered Nov 12 '22 14:11

IanS


Currently I'm using:

assert 'datetime64' in str(comp.dtype)

It leaves a bad taste in my mouth (since it relies on other behavior), but its all I can some up with.

like image 22
JudoWill Avatar answered Nov 12 '22 14:11

JudoWill