Skip to content

Commit 95140e6

Browse files
committed
Fix pandas-dev#17965 datetime64 comparison
1 parent 8dac633 commit 95140e6

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

doc/source/whatsnew/v0.21.1.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Bug Fixes
6161
- Bug in :class:`DatetimeIndex` subtracting datetimelike from DatetimeIndex could fail to overflow (:issue:`18020`)
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`)
64+
- Bug in ``_libs.index.convert_scalar()`` when comparing datetimes to np.datetime64 (:issue:`17965`)
6465

6566
Conversion
6667
^^^^^^^^^^

pandas/_libs/index.pyx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,8 @@ cpdef convert_scalar(ndarray arr, object value):
549549
if arr.descr.type_num == NPY_DATETIME:
550550
if isinstance(value, np.ndarray):
551551
pass
552+
if isinstance(value, np.datetime64):
553+
return Timestamp(value).value
552554
elif isinstance(value, datetime):
553555
return Timestamp(value).value
554556
elif value is None or value != value:

pandas/tests/indexes/datetimes/test_partial_slicing.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,3 +330,9 @@ def test_loc_datetime_length_one(self):
330330

331331
result = df.loc['2016-10-01T00:00:00':]
332332
tm.assert_frame_equal(result, df)
333+
334+
def test_comparison_to_datetime64(self):
335+
# GH issue #17965
336+
df = DataFrame({'dates': date_range('2000-01-01', '2000-01-05')})
337+
date = df['dates'].unique()[0]
338+
tm.assert_frame_equal(df.loc[df['dates'] == date], df.iloc[:1])

0 commit comments

Comments
 (0)