Skip to content

Commit f1cfe5b

Browse files
committed
CLN: remove simple _DATELIKE_DTYPES test and replace with is_datetimelike accessor
this was some older code Author: Jeff Reback <[email protected]> Closes #14909 from jreback/clean_datelike and squashes the following commits: 58ff2c4 [Jeff Reback] CLN: remove simple _DATELIKE_DTYPES test and replace with is_datetimelike accessor
1 parent a718962 commit f1cfe5b

File tree

4 files changed

+12
-15
lines changed

4 files changed

+12
-15
lines changed

pandas/core/groupby.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
from pandas.compat.numpy import function as nv
1515
from pandas.compat.numpy import _np_version_under1p8
1616

17-
from pandas.types.common import (_DATELIKE_DTYPES,
18-
is_numeric_dtype,
17+
from pandas.types.common import (is_numeric_dtype,
1918
is_timedelta64_dtype, is_datetime64_dtype,
2019
is_categorical_dtype,
20+
is_datetimelike,
2121
is_datetime_or_timedelta_dtype,
2222
is_bool, is_integer_dtype,
2323
is_complex_dtype,
@@ -3453,10 +3453,10 @@ def first_non_None_value(values):
34533453
# if we have date/time like in the original, then coerce dates
34543454
# as we are stacking can easily have object dtypes here
34553455
so = self._selected_obj
3456-
if (so.ndim == 2 and so.dtypes.isin(_DATELIKE_DTYPES).any()):
3456+
if (so.ndim == 2 and so.dtypes.apply(is_datetimelike).any()):
34573457
result = result._convert(numeric=True)
34583458
date_cols = self._selected_obj.select_dtypes(
3459-
include=list(_DATELIKE_DTYPES)).columns
3459+
include=['datetime', 'timedelta']).columns
34603460
date_cols = date_cols.intersection(result.columns)
34613461
result[date_cols] = (result[date_cols]
34623462
._convert(datetime=True,

pandas/types/cast.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from pandas.compat import string_types, text_type, PY3
88
from .common import (_ensure_object, is_bool, is_integer, is_float,
99
is_complex, is_datetimetz, is_categorical_dtype,
10+
is_datetimelike,
1011
is_extension_type, is_object_dtype,
1112
is_datetime64tz_dtype, is_datetime64_dtype,
1213
is_timedelta64_dtype, is_dtype_equal,
@@ -18,7 +19,7 @@
1819
_ensure_int8, _ensure_int16,
1920
_ensure_int32, _ensure_int64,
2021
_NS_DTYPE, _TD_DTYPE, _INT64_DTYPE,
21-
_DATELIKE_DTYPES, _POSSIBLY_CAST_DTYPES)
22+
_POSSIBLY_CAST_DTYPES)
2223
from .dtypes import ExtensionDtype
2324
from .generic import ABCDatetimeIndex, ABCPeriodIndex, ABCSeries
2425
from .missing import isnull, notnull
@@ -164,7 +165,7 @@ def _maybe_upcast_putmask(result, mask, other):
164165
# in np.place:
165166
# NaN -> NaT
166167
# integer or integer array -> date-like array
167-
if result.dtype in _DATELIKE_DTYPES:
168+
if is_datetimelike(result.dtype):
168169
if is_scalar(other):
169170
if isnull(other):
170171
other = result.dtype.type('nat')
@@ -666,7 +667,7 @@ def _possibly_castable(arr):
666667
# otherwise try to coerce
667668
kind = arr.dtype.kind
668669
if kind == 'M' or kind == 'm':
669-
return arr.dtype in _DATELIKE_DTYPES
670+
return is_datetime64_dtype(arr.dtype)
670671

671672
return arr.dtype.name not in _POSSIBLY_CAST_DTYPES
672673

pandas/types/common.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@
2323
_TD_DTYPE = np.dtype('m8[ns]')
2424
_INT64_DTYPE = np.dtype(np.int64)
2525

26-
_DATELIKE_DTYPES = set([np.dtype(t)
27-
for t in ['M8[ns]', '<M8[ns]', '>M8[ns]',
28-
'm8[ns]', '<m8[ns]', '>m8[ns]']])
29-
3026
_ensure_float64 = algos.ensure_float64
3127
_ensure_float32 = algos.ensure_float32
3228

@@ -127,7 +123,8 @@ def is_datetime_arraylike(arr):
127123

128124

129125
def is_datetimelike(arr):
130-
return (arr.dtype in _DATELIKE_DTYPES or
126+
return (is_datetime64_dtype(arr) or is_datetime64tz_dtype(arr) or
127+
is_timedelta64_dtype(arr) or
131128
isinstance(arr, ABCPeriodIndex) or
132129
is_datetimetz(arr))
133130

pandas/types/missing.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
is_object_dtype,
2020
is_integer,
2121
_TD_DTYPE,
22-
_NS_DTYPE,
23-
_DATELIKE_DTYPES)
22+
_NS_DTYPE)
2423
from .inference import is_list_like
2524

2625

@@ -169,7 +168,7 @@ def _isnull_ndarraylike_old(obj):
169168
vec = lib.isnullobj_old(values.ravel())
170169
result[:] = vec.reshape(shape)
171170

172-
elif dtype in _DATELIKE_DTYPES:
171+
elif is_datetime64_dtype(dtype):
173172
# this is the NaT pattern
174173
result = values.view('i8') == iNaT
175174
else:

0 commit comments

Comments
 (0)