Skip to content

BUG: Do not use string Index like Datetimelike Index #33531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 30 additions & 26 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,16 @@ def equals(self, other) -> bool:
if not isinstance(other, ABCIndexClass):
return False
elif not isinstance(other, type(self)):
try:
other = type(self)(other)
except (ValueError, TypeError, OverflowError):
# e.g.
# ValueError -> cannot parse str entry, or OutOfBoundsDatetime
# TypeError -> trying to convert IntervalIndex to DatetimeIndex
# OverflowError -> Index([very_large_timedeltas])
if self._is_convertible_to_index_for_join(other):
try:
other = type(self)(other)
except (ValueError, TypeError, OverflowError):
# e.g.
# ValueError -> cannot parse str entry, or OutOfBoundsDatetime
# TypeError -> trying to convert IntervalIndex to DatetimeIndex
# OverflowError -> Index([very_large_timedeltas])
return False
else:
return False

if not is_dtype_equal(self.dtype, other.dtype):
Expand Down Expand Up @@ -596,6 +599,26 @@ def _convert_arr_indexer(self, keyarr):
converted_arr = com.asarray_tuplesafe(keyarr)
return converted_arr

@classmethod
def _is_convertible_to_index_for_join(cls, other: Index) -> bool:
"""
return a boolean whether I can attempt conversion to a
DatetimeIndex/TimedeltaIndex
"""
if isinstance(other, cls):
return False
elif len(other) > 0 and other.inferred_type not in (
"floating",
"mixed-integer",
"integer",
"integer-na",
"mixed-integer-float",
"mixed",
"string",
):
return True
return False


class DatetimeTimedeltaMixin(DatetimeIndexOpsMixin, Int64Index):
"""
Expand Down Expand Up @@ -884,25 +907,6 @@ def _maybe_utc_convert(self, other):
other = other.tz_convert("UTC")
return this, other

@classmethod
def _is_convertible_to_index_for_join(cls, other: Index) -> bool:
"""
return a boolean whether I can attempt conversion to a
DatetimeIndex/TimedeltaIndex
"""
if isinstance(other, cls):
return False
elif len(other) > 0 and other.inferred_type not in (
"floating",
"mixed-integer",
"integer",
"integer-na",
"mixed-integer-float",
"mixed",
):
return True
return False

# --------------------------------------------------------------------
# List-Like Methods

Expand Down
17 changes: 17 additions & 0 deletions pandas/tests/indexes/datetimes/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,3 +408,20 @@ def test_isocalendar_returns_correct_values_close_to_new_year_with_tz():
dtype="UInt32",
)
tm.assert_frame_equal(result, expected_data_frame)


def test_datetimelike_string():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally could paramaterize this with things that don't match here (e.g. more than just 1 thing).

Also this is not in the right location, search for were we test .equals

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will move this in pandas/tests/indexes/datetimes/test_ops.py

By trying to parametrize I have now more question about the expected behaviors, see comment that follows.

# Related to PR 32739
# Ensure we do not compare strings and datetimelike type.
date_string = "2020-04-13"
i1 = pd.Index([date_string])
i2 = pd.Index([pd.to_datetime(date_string)])

assert i1.equals(i2) is False
assert i2.equals(i1) is False

assert len(i1.intersection(i2)) == 0
assert len(i2.intersection(i1)) == 0

assert len(i1.union(i2)) == 2
assert len(i2.union(i1)) == 2