Skip to content

Commit d442a9f

Browse files
committed
BUG: Do not use string Index like Datetimelike Index
1 parent 455de19 commit d442a9f

File tree

2 files changed

+47
-26
lines changed

2 files changed

+47
-26
lines changed

pandas/core/indexes/datetimelike.py

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,16 @@ def equals(self, other) -> bool:
142142
if not isinstance(other, ABCIndexClass):
143143
return False
144144
elif not isinstance(other, type(self)):
145-
try:
146-
other = type(self)(other)
147-
except (ValueError, TypeError, OverflowError):
148-
# e.g.
149-
# ValueError -> cannot parse str entry, or OutOfBoundsDatetime
150-
# TypeError -> trying to convert IntervalIndex to DatetimeIndex
151-
# OverflowError -> Index([very_large_timedeltas])
145+
if self._is_convertible_to_index_for_join(other):
146+
try:
147+
other = type(self)(other)
148+
except (ValueError, TypeError, OverflowError):
149+
# e.g.
150+
# ValueError -> cannot parse str entry, or OutOfBoundsDatetime
151+
# TypeError -> trying to convert IntervalIndex to DatetimeIndex
152+
# OverflowError -> Index([very_large_timedeltas])
153+
return False
154+
else:
152155
return False
153156

154157
if not is_dtype_equal(self.dtype, other.dtype):
@@ -602,6 +605,26 @@ def delete(self, loc):
602605
arr = type(self._data)._simple_new(new_i8s, dtype=self.dtype, freq=freq)
603606
return type(self)._simple_new(arr, name=self.name)
604607

608+
@classmethod
609+
def _is_convertible_to_index_for_join(cls, other: Index) -> bool:
610+
"""
611+
return a boolean whether I can attempt conversion to a
612+
DatetimeIndex/TimedeltaIndex
613+
"""
614+
if isinstance(other, cls):
615+
return False
616+
elif len(other) > 0 and other.inferred_type not in (
617+
"floating",
618+
"mixed-integer",
619+
"integer",
620+
"integer-na",
621+
"mixed-integer-float",
622+
"mixed",
623+
"string",
624+
):
625+
return True
626+
return False
627+
605628

606629
class DatetimeTimedeltaMixin(DatetimeIndexOpsMixin, Int64Index):
607630
"""
@@ -873,25 +896,6 @@ def _maybe_utc_convert(self, other):
873896
other = other.tz_convert("UTC")
874897
return this, other
875898

876-
@classmethod
877-
def _is_convertible_to_index_for_join(cls, other: Index) -> bool:
878-
"""
879-
return a boolean whether I can attempt conversion to a
880-
DatetimeIndex/TimedeltaIndex
881-
"""
882-
if isinstance(other, cls):
883-
return False
884-
elif len(other) > 0 and other.inferred_type not in (
885-
"floating",
886-
"mixed-integer",
887-
"integer",
888-
"integer-na",
889-
"mixed-integer-float",
890-
"mixed",
891-
):
892-
return True
893-
return False
894-
895899
def _wrap_joined_index(self, joined: np.ndarray, other):
896900
assert other.dtype == self.dtype, (other.dtype, self.dtype)
897901
name = get_op_result_name(self, other)

pandas/tests/indexes/datetimes/test_misc.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,3 +373,20 @@ def test_iter_readonly():
373373
arr.setflags(write=False)
374374
dti = pd.to_datetime(arr)
375375
list(dti)
376+
377+
378+
def test_datetimelike_string():
379+
# Related to PR 32739
380+
# Ensure we do not compare strings and datetimelike type.
381+
date_string = "2020-04-13"
382+
i1 = pd.Index([date_string])
383+
i2 = pd.Index([pd.to_datetime(date_string)])
384+
385+
assert i1.equals(i2) is False
386+
assert i2.equals(i1) is False
387+
388+
assert len(i1.intersection(i2)) == 0
389+
assert len(i2.intersection(i1)) == 0
390+
391+
assert len(i1.union(i2)) == 2
392+
assert len(i2.union(i1)) == 2

0 commit comments

Comments
 (0)