Skip to content

Commit 5e1a1fd

Browse files
committed
BUG: Fix mixed datetime dtype inference
1 parent 837daf1 commit 5e1a1fd

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

doc/source/whatsnew/v1.1.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@ Datetimelike
461461
- Bug in :meth:`DatetimeIndex.to_period` not infering the frequency when called with no arguments (:issue:`33358`)
462462
- Bug in :meth:`DatetimeIndex.tz_localize` incorrectly retaining ``freq`` in some cases where the original freq is no longer valid (:issue:`30511`)
463463
- Bug in :meth:`DatetimeIndex.intersection` losing ``freq`` and timezone in some cases (:issue:`33604`)
464+
- Bug in :meth:`Index.get_indexer` where incorrect output would be returned for mixed datetime-like targets (:issues:`33741`)
464465

465466
Timedelta
466467
^^^^^^^^^

pandas/_libs/lib.pyx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,8 @@ def infer_dtype(value: object, skipna: bool = True) -> str:
13731373
elif PyDateTime_Check(val):
13741374
if is_datetime_array(values):
13751375
return "datetime"
1376+
if is_date_array(values):
1377+
return "date"
13761378

13771379
elif PyDate_Check(val):
13781380
if is_date_array(values, skipna=skipna):

pandas/tests/dtypes/test_inference.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,18 @@ def test_date(self):
10971097
result = lib.infer_dtype(dates, skipna=True)
10981098
assert result == "date"
10991099

1100+
@pytest.mark.parametrize(
1101+
"values",
1102+
[
1103+
[date(2020, 1, 1), pd.Timestamp("2020-01-01")],
1104+
[pd.Timestamp("2020-01-01"), date(2020, 1, 1)],
1105+
],
1106+
)
1107+
def test_infer_dtype_date_order_invariant(self, values):
1108+
# https://github.com/pandas-dev/pandas/issues/33741
1109+
result = lib.infer_dtype(values, skipna=True)
1110+
assert result == "date"
1111+
11001112
def test_is_numeric_array(self):
11011113

11021114
assert lib.is_float_array(np.array([1, 2.0]))

pandas/tests/indexes/datetimes/test_indexing.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from datetime import datetime, time, timedelta
1+
from datetime import datetime, date, time, timedelta
22

33
import numpy as np
44
import pytest
@@ -547,6 +547,22 @@ def test_get_indexer(self):
547547
with pytest.raises(ValueError, match="abbreviation w/o a number"):
548548
idx.get_indexer(idx[[0]], method="nearest", tolerance="foo")
549549

550+
@pytest.mark.parametrize(
551+
"target",
552+
[
553+
[date(2020, 1, 1), pd.Timestamp("2020-01-02")],
554+
[pd.Timestamp("2020-01-01"), date(2020, 1, 2)],
555+
],
556+
)
557+
def test_get_indexer_mixed_dtypes(self, target):
558+
# https://github.com/pandas-dev/pandas/issues/33741
559+
values = pd.DatetimeIndex(
560+
[pd.Timestamp("2020-01-01"), pd.Timestamp("2020-01-02")]
561+
)
562+
result = values.get_indexer(target)
563+
expected = np.array([0, 1])
564+
tm.assert_numpy_array_equal(result, expected)
565+
550566

551567
class TestMaybeCastSliceBound:
552568
def test_maybe_cast_slice_bounds_empty(self):

0 commit comments

Comments
 (0)