Skip to content

Commit ee68bb3

Browse files
watercrossingTomAugspurger
authored andcommitted
Fix #17965 to allow full comparison of datetimelike objects (#18188)
(cherry picked from commit 77f10f0)
1 parent 535b0a2 commit ee68bb3

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
@@ -73,6 +73,7 @@ Conversion
7373
Indexing
7474
^^^^^^^^
7575

76+
- Bug in a boolean comparison of a ``datetime.datetime`` and a ``datetime64[ns]`` dtype Series (:issue:`17965`)
7677
- Bug where a ``MultiIndex`` with more than a million records was not raising ``AttributeError`` when trying to access a missing attribute (:issue:`18165`)
7778
-
7879
-

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, 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

@@ -500,7 +500,7 @@ cpdef convert_scalar(ndarray arr, object value):
500500
if arr.descr.type_num == NPY_DATETIME:
501501
if isinstance(value, np.ndarray):
502502
pass
503-
elif isinstance(value, datetime):
503+
elif isinstance(value, (datetime, np.datetime64, date)):
504504
return Timestamp(value).value
505505
elif value is None or value != value:
506506
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)
@@ -268,3 +269,21 @@ def test_loc_datetime_length_one(self):
268269

269270
result = df.loc['2016-10-01T00:00:00':]
270271
tm.assert_frame_equal(result, df)
272+
273+
@pytest.mark.parametrize('datetimelike', [
274+
Timestamp('20130101'), datetime(2013, 1, 1),
275+
date(2013, 1, 1), np.datetime64('2013-01-01T00:00', 'ns')])
276+
@pytest.mark.parametrize('op,expected', [
277+
(op.lt, [True, False, False, False]),
278+
(op.le, [True, True, False, False]),
279+
(op.eq, [False, True, False, False]),
280+
(op.gt, [False, False, False, True])])
281+
def test_selection_by_datetimelike(self, datetimelike, op, expected):
282+
# GH issue #17965, test for ability to compare datetime64[ns] columns
283+
# to datetimelike
284+
df = DataFrame({'A': [pd.Timestamp('20120101'),
285+
pd.Timestamp('20130101'),
286+
np.nan, pd.Timestamp('20130103')]})
287+
result = op(df.A, datetimelike)
288+
expected = Series(expected, name='A')
289+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)