Skip to content

Commit

Permalink
Short circuit Index.equal if compared Index isn't same type (#18067)
Browse files Browse the repository at this point in the history
closes #8689

Before, comparing two different Index subclasses would execute a GPU kernel when we know they wouldn't be equal (e.g. DatetimeIndex equals RangeIndex). This PR add a short circuit clause to check that we are comparing the same subclasses.

Also ensures we don't return a `np.bool_` object from this result.

Authors:
  - Matthew Roeschke (https://github.com/mroeschke)

Approvers:
  - GALI PREM SAGAR (https://github.com/galipremsagar)

URL: #18067
  • Loading branch information
mroeschke authored Feb 26, 2025
1 parent 72d5792 commit e5d866b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion python/cudf/cudf/core/column/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ def all(self, skipna: bool = True) -> bool:
# is empty.
if self.null_count == self.size:
return True
return self.reduce("all")
return bool(self.reduce("all"))

def any(self, skipna: bool = True) -> bool:
# Early exit for fast cases.
Expand Down
9 changes: 9 additions & 0 deletions python/cudf/cudf/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,15 @@ def equals(self, other) -> bool:
elif other_is_categorical and not self_is_categorical:
self = self.astype(other.dtype)
check_dtypes = True
elif (
not self_is_categorical
and not other_is_categorical
and not isinstance(other, RangeIndex)
and not isinstance(self, type(other))
):
# Can compare Index to CategoricalIndex or RangeIndex
# Other comparisons are invalid
return False

try:
return self._column.equals(
Expand Down

0 comments on commit e5d866b

Please sign in to comment.