Skip to content

Commit 4e2b5d6

Browse files
committed
handle min/max correctly for datetimelikes
1 parent dcf4fb4 commit 4e2b5d6

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

pandas/core/arrays/base.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -735,9 +735,7 @@ def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None,
735735
-------
736736
scalar
737737
"""
738-
739-
# we dispatchh to the nanops operations
740-
return op(self, axis=axis, skipna=skipna)
738+
raise AbstractMethodError(self)
741739

742740

743741
class ExtensionScalarOpsMixin(ExtensionOpsMixin):

pandas/core/arrays/datetimelike.py

+36
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,42 @@ def _validate_frequency(cls, index, freq, **kwargs):
341341
'does not conform to passed frequency {passed}'
342342
.format(infer=inferred, passed=freq.freqstr))
343343

344+
# ------------------------------------------------------------------
345+
# Reduction Methods
346+
347+
def _reduce(self, op, name, axis=0, skipna=True, numeric_only=None,
348+
filter_type=None, **kwargs):
349+
"""Return a scalar result of performing the op
350+
351+
Parameters
352+
----------
353+
op : callable
354+
function to apply to the array
355+
name : str
356+
name of the function
357+
axis : int, default 0
358+
axis over which to apply, defined as 0 currently
359+
skipna : bool, default True
360+
if True, skip NaN values
361+
numeric_only : bool, optional
362+
if True, only perform numeric ops
363+
filter_type : str, optional
364+
kwargs : dict
365+
366+
Returns
367+
-------
368+
scalar
369+
"""
370+
# if we have a reduction op already defined, use it
371+
# this is important for min/max where tz's must be preserved
372+
# and nanops is not geared towards this
373+
# TODO(jreback): we are ignoring skipna
374+
if hasattr(self, name):
375+
return getattr(self, name)()
376+
377+
# we dispatch to the nanops operations
378+
return op(self, axis=axis, skipna=skipna)
379+
344380
# ------------------------------------------------------------------
345381
# Arithmetic Methods
346382

pandas/core/dtypes/common.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1236,7 +1236,8 @@ def is_datetime_or_timedelta_dtype(arr_or_dtype):
12361236
if arr_or_dtype is None:
12371237
return False
12381238
tipo = _get_dtype_type(arr_or_dtype)
1239-
return issubclass(tipo, (np.datetime64, np.timedelta64))
1239+
return (issubclass(tipo, (np.datetime64, np.timedelta64)) or
1240+
is_datetime64tz_dtype(arr_or_dtype))
12401241

12411242

12421243
def _is_unorderable_exception(e):

pandas/tests/dtypes/test_common.py

+2
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,8 @@ def test_is_datetime_or_timedelta_dtype():
388388
assert not com.is_datetime_or_timedelta_dtype(np.array(['a', 'b']))
389389

390390
assert com.is_datetime_or_timedelta_dtype(np.datetime64)
391+
assert com.is_datetime_or_timedelta_dtype(
392+
DatetimeTZDtype("ns", "US/Eastern"))
391393
assert com.is_datetime_or_timedelta_dtype(np.timedelta64)
392394
assert com.is_datetime_or_timedelta_dtype(
393395
np.array([], dtype=np.timedelta64))

0 commit comments

Comments
 (0)