Skip to content

Commit ebe805b

Browse files
committed
Fix #17965 to allow full comparison of datetimelike objects
1 parent 9050e38 commit ebe805b

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

doc/source/whatsnew/v0.21.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Bug Fixes
6262
- Bug in ``pd.Series.rolling.skew()`` and ``rolling.kurt()`` with all equal values has floating issue (:issue:`18044`)
6363
- Bug in ``pd.DataFrameGroupBy.count()`` when counting over a datetimelike column (:issue:`13393`)
6464
- Bug in ``pd.concat`` when empty and non-empty DataFrames or Series are concatenated (:issue:`18178` :issue:`18187`)
65+
- Bug in a boolean comparison with a ``datetime.datetime``, ``datetime.date``, ``np.datetime64`` and a ``datetime64[ns]`` dtype Series (:issue:`17965`)
6566

6667
Conversion
6768
^^^^^^^^^^

pandas/_libs/index.pyx

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ from hashtable cimport HashTable
1919

2020
from pandas._libs import algos, period as periodlib, hashtable as _hash
2121
from pandas._libs.tslib import Timestamp, Timedelta
22-
from datetime import datetime, timedelta
22+
from datetime import datetime, timedelta, date
2323

2424
from cpython cimport PyTuple_Check, PyList_Check
2525

@@ -549,7 +549,7 @@ cpdef convert_scalar(ndarray arr, object value):
549549
if arr.descr.type_num == NPY_DATETIME:
550550
if isinstance(value, np.ndarray):
551551
pass
552-
elif isinstance(value, datetime):
552+
elif isinstance(value, (datetime, np.datetime64, date)):
553553
return Timestamp(value).value
554554
elif value is None or value != value:
555555
return iNaT

pandas/tests/indexes/datetimes/test_partial_slicing.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
import pytest
44

5-
from datetime import datetime
5+
from datetime import datetime, date
66
import numpy as np
77
import pandas as pd
8+
import operator as op
89

910
from pandas import (DatetimeIndex, Series, DataFrame,
1011
date_range, Index, Timedelta, Timestamp)
@@ -330,3 +331,21 @@ def test_loc_datetime_length_one(self):
330331

331332
result = df.loc['2016-10-01T00:00:00':]
332333
tm.assert_frame_equal(result, df)
334+
335+
@pytest.mark.parametrize('datetimelike', [
336+
Timestamp('20130101'), datetime(2013, 1, 1),
337+
date(2013, 1, 1), np.datetime64('2013-01-01', 'ns')])
338+
@pytest.mark.parametrize('op,expected', [
339+
(op.lt, [True, False, False, False]),
340+
(op.le, [True, True, False, False]),
341+
(op.eq, [False, True, False, False]),
342+
(op.gt, [False, False, False, True])])
343+
def test_selection_by_datetimelike(self, datetimelike, op, expected):
344+
# GH issue #17965, test for ability to compare datetime64[ns] columns
345+
# to datetimelike
346+
df = DataFrame({'A': [pd.Timestamp('20120101'),
347+
pd.Timestamp('20130101'),
348+
np.nan, pd.Timestamp('20130103')]})
349+
result = op(df.A, datetimelike)
350+
expected = Series(expected, name='A')
351+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)