From 58ff2c442f88bc94f075ed074b8ad25efd1e46e6 Mon Sep 17 00:00:00 2001 From: Jeff Reback Date: Sun, 18 Dec 2016 14:56:37 -0500 Subject: [PATCH] CLN: remove simple _DATELIKE_DTYPES test and replace with is_datetimelike accessor --- pandas/core/groupby.py | 8 ++++---- pandas/types/cast.py | 7 ++++--- pandas/types/common.py | 7 ++----- pandas/types/missing.py | 5 ++--- 4 files changed, 12 insertions(+), 15 deletions(-) diff --git a/pandas/core/groupby.py b/pandas/core/groupby.py index b249cded39133..950ad53abe5e0 100644 --- a/pandas/core/groupby.py +++ b/pandas/core/groupby.py @@ -14,10 +14,10 @@ from pandas.compat.numpy import function as nv from pandas.compat.numpy import _np_version_under1p8 -from pandas.types.common import (_DATELIKE_DTYPES, - is_numeric_dtype, +from pandas.types.common import (is_numeric_dtype, is_timedelta64_dtype, is_datetime64_dtype, is_categorical_dtype, + is_datetimelike, is_datetime_or_timedelta_dtype, is_bool, is_integer_dtype, is_complex_dtype, @@ -3453,10 +3453,10 @@ def first_non_None_value(values): # if we have date/time like in the original, then coerce dates # as we are stacking can easily have object dtypes here so = self._selected_obj - if (so.ndim == 2 and so.dtypes.isin(_DATELIKE_DTYPES).any()): + if (so.ndim == 2 and so.dtypes.apply(is_datetimelike).any()): result = result._convert(numeric=True) date_cols = self._selected_obj.select_dtypes( - include=list(_DATELIKE_DTYPES)).columns + include=['datetime', 'timedelta']).columns date_cols = date_cols.intersection(result.columns) result[date_cols] = (result[date_cols] ._convert(datetime=True, diff --git a/pandas/types/cast.py b/pandas/types/cast.py index d4beab5655e5c..4f4f95d5a455b 100644 --- a/pandas/types/cast.py +++ b/pandas/types/cast.py @@ -7,6 +7,7 @@ from pandas.compat import string_types, text_type, PY3 from .common import (_ensure_object, is_bool, is_integer, is_float, is_complex, is_datetimetz, is_categorical_dtype, + is_datetimelike, is_extension_type, is_object_dtype, is_datetime64tz_dtype, is_datetime64_dtype, is_timedelta64_dtype, is_dtype_equal, @@ -18,7 +19,7 @@ _ensure_int8, _ensure_int16, _ensure_int32, _ensure_int64, _NS_DTYPE, _TD_DTYPE, _INT64_DTYPE, - _DATELIKE_DTYPES, _POSSIBLY_CAST_DTYPES) + _POSSIBLY_CAST_DTYPES) from .dtypes import ExtensionDtype from .generic import ABCDatetimeIndex, ABCPeriodIndex, ABCSeries from .missing import isnull, notnull @@ -164,7 +165,7 @@ def _maybe_upcast_putmask(result, mask, other): # in np.place: # NaN -> NaT # integer or integer array -> date-like array - if result.dtype in _DATELIKE_DTYPES: + if is_datetimelike(result.dtype): if is_scalar(other): if isnull(other): other = result.dtype.type('nat') @@ -666,7 +667,7 @@ def _possibly_castable(arr): # otherwise try to coerce kind = arr.dtype.kind if kind == 'M' or kind == 'm': - return arr.dtype in _DATELIKE_DTYPES + return is_datetime64_dtype(arr.dtype) return arr.dtype.name not in _POSSIBLY_CAST_DTYPES diff --git a/pandas/types/common.py b/pandas/types/common.py index 5d161efa838de..b9d4c112c00d6 100644 --- a/pandas/types/common.py +++ b/pandas/types/common.py @@ -23,10 +23,6 @@ _TD_DTYPE = np.dtype('m8[ns]') _INT64_DTYPE = np.dtype(np.int64) -_DATELIKE_DTYPES = set([np.dtype(t) - for t in ['M8[ns]', 'M8[ns]', - 'm8[ns]', 'm8[ns]']]) - _ensure_float64 = algos.ensure_float64 _ensure_float32 = algos.ensure_float32 @@ -127,7 +123,8 @@ def is_datetime_arraylike(arr): def is_datetimelike(arr): - return (arr.dtype in _DATELIKE_DTYPES or + return (is_datetime64_dtype(arr) or is_datetime64tz_dtype(arr) or + is_timedelta64_dtype(arr) or isinstance(arr, ABCPeriodIndex) or is_datetimetz(arr)) diff --git a/pandas/types/missing.py b/pandas/types/missing.py index a4af127e0c381..e6791b79bf3bd 100644 --- a/pandas/types/missing.py +++ b/pandas/types/missing.py @@ -19,8 +19,7 @@ is_object_dtype, is_integer, _TD_DTYPE, - _NS_DTYPE, - _DATELIKE_DTYPES) + _NS_DTYPE) from .inference import is_list_like @@ -169,7 +168,7 @@ def _isnull_ndarraylike_old(obj): vec = lib.isnullobj_old(values.ravel()) result[:] = vec.reshape(shape) - elif dtype in _DATELIKE_DTYPES: + elif is_datetime64_dtype(dtype): # this is the NaT pattern result = values.view('i8') == iNaT else: