Skip to content

Commit 9fc201d

Browse files
committed
BUG: Do not use string Index like Datetimelike Index
1 parent 624a1be commit 9fc201d

File tree

2 files changed

+56
-7
lines changed

2 files changed

+56
-7
lines changed

pandas/core/indexes/datetimelike.py

+39-7
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,16 @@ def equals(self, other) -> bool:
138138
if not isinstance(other, ABCIndexClass):
139139
return False
140140
elif not isinstance(other, type(self)):
141-
try:
142-
other = type(self)(other)
143-
except (ValueError, TypeError, OverflowError):
144-
# e.g.
145-
# ValueError -> cannot parse str entry, or OutOfBoundsDatetime
146-
# TypeError -> trying to convert IntervalIndex to DatetimeIndex
147-
# OverflowError -> Index([very_large_timedeltas])
141+
if self._is_convertible_to_index_for_join(other):
142+
try:
143+
other = type(self)(other)
144+
except (ValueError, TypeError, OverflowError):
145+
# e.g.
146+
# ValueError -> cannot parse str entry, or OutOfBoundsDatetime
147+
# TypeError -> trying to convert IntervalIndex to DatetimeIndex
148+
# OverflowError -> Index([very_large_timedeltas])
149+
return False
150+
else:
148151
return False
149152

150153
if not is_dtype_equal(self.dtype, other.dtype):
@@ -596,6 +599,26 @@ def _convert_arr_indexer(self, keyarr):
596599
converted_arr = com.asarray_tuplesafe(keyarr)
597600
return converted_arr
598601

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

600623
class DatetimeTimedeltaMixin(DatetimeIndexOpsMixin, Int64Index):
601624
"""
@@ -903,6 +926,15 @@ def _is_convertible_to_index_for_join(cls, other: Index) -> bool:
903926
return True
904927
return False
905928

929+
def _wrap_joined_index(self, joined: np.ndarray, other):
930+
assert other.dtype == self.dtype, (other.dtype, self.dtype)
931+
name = get_op_result_name(self, other)
932+
933+
freq = self.freq if self._can_fast_union(other) else None
934+
new_data = type(self._data)._simple_new(joined, dtype=self.dtype, freq=freq)
935+
936+
return type(self)._simple_new(new_data, name=name)
937+
906938
# --------------------------------------------------------------------
907939
# List-Like Methods
908940

pandas/tests/indexes/datetimes/test_misc.py

+17
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,20 @@ def test_isocalendar_returns_correct_values_close_to_new_year_with_tz():
408408
dtype="UInt32",
409409
)
410410
tm.assert_frame_equal(result, expected_data_frame)
411+
412+
413+
def test_datetimelike_string():
414+
# Related to PR 32739
415+
# Ensure we do not compare strings and datetimelike type.
416+
date_string = "2020-04-13"
417+
i1 = pd.Index([date_string])
418+
i2 = pd.Index([pd.to_datetime(date_string)])
419+
420+
assert i1.equals(i2) is False
421+
assert i2.equals(i1) is False
422+
423+
assert len(i1.intersection(i2)) == 0
424+
assert len(i2.intersection(i1)) == 0
425+
426+
assert len(i1.union(i2)) == 2
427+
assert len(i2.union(i1)) == 2

0 commit comments

Comments
 (0)