From 824129eff89b80537b3036ea74ba541839c0e68c Mon Sep 17 00:00:00 2001 From: Joe Jevnik Date: Tue, 28 Feb 2017 22:49:17 -0500 Subject: [PATCH] ENH: make NaT_for_dtype coerce the input to a dtype --- zipline/utils/numpy_utils.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/zipline/utils/numpy_utils.py b/zipline/utils/numpy_utils.py index 77371da6..0a13b08f 100644 --- a/zipline/utils/numpy_utils.py +++ b/zipline/utils/numpy_utils.py @@ -8,6 +8,7 @@ from warnings import ( filterwarnings, ) +import numpy as np from numpy import ( broadcast, busday_count, @@ -49,7 +50,24 @@ NaTmap = { dtype('datetime64[%s]' % unit): datetime64('NaT', unit) for unit in ('ns', 'us', 'ms', 's', 'm', 'D') } -NaT_for_dtype = NaTmap.__getitem__ + + +def NaT_for_dtype(dtype): + """Retrieve NaT with the same units as ``dtype``. + + Parameters + ---------- + dtype : dtype-coercable + The dtype to lookup the NaT value for. + + Returns + ------- + NaT : dtype + The NaT value for the given dtype. + """ + return NaTmap[np.dtype(dtype)] + + NaTns = NaT_for_dtype(datetime64ns_dtype) NaTD = NaT_for_dtype(datetime64D_dtype)