Skip to content

Commit 5717741

Browse files
committed
Try except
1 parent a25ce87 commit 5717741

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

pandas/core/indexes/datetimes.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from pandas._libs import NaT, Period, Timestamp, index as libindex, lib, tslib as libts
99
from pandas._libs.tslibs import fields, parsing, timezones
10+
from pandas._libs.tslibs.np_datetime import OutOfBoundsDatetime
1011
from pandas._typing import DtypeObj, Label
1112
from pandas.util._decorators import cache_readonly
1213

@@ -540,7 +541,10 @@ def _validate_partial_date_slice(self, reso: str):
540541

541542
def _maybe_promote(self, other):
542543
if other.inferred_type == "date":
543-
other = DatetimeIndex(other)
544+
try:
545+
other = DatetimeIndex(other)
546+
except OutOfBoundsDatetime:
547+
pass
544548
return self, other
545549

546550
def get_loc(self, key, method=None, tolerance=None):

pandas/tests/indexes/datetimes/test_indexing.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,22 @@ def test_get_indexer_mixed_dtypes(self, target):
563563
expected = np.array([0, 1], dtype=np.intp)
564564
tm.assert_numpy_array_equal(result, expected)
565565

566+
@pytest.mark.parametrize(
567+
"target, positions",
568+
[
569+
([date(9999, 1, 1), pd.Timestamp("2020-01-01")], [-1, 0]),
570+
([pd.Timestamp("2020-01-01"), date(9999, 1, 1)], [0, -1]),
571+
([date(9999, 1, 1), date(9999, 1, 1)], [-1, -1]),
572+
],
573+
)
574+
def test_get_indexer_out_of_bounds_date(self, target, positions):
575+
values = pd.DatetimeIndex(
576+
[pd.Timestamp("2020-01-01"), pd.Timestamp("2020-01-02")]
577+
)
578+
result = values.get_indexer(target)
579+
expected = np.array(positions, dtype=np.intp)
580+
tm.assert_numpy_array_equal(result, expected)
581+
566582

567583
class TestMaybeCastSliceBound:
568584
def test_maybe_cast_slice_bounds_empty(self):

0 commit comments

Comments
 (0)