diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 6b71e455782e3..3b28361399fba 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -220,7 +220,7 @@ def _outer_indexer(self, left, right): _typ = "index" _data: Union[ExtensionArray, np.ndarray] - _id: _Identity + _id: Optional[_Identity] = None _name: Label = None # MultiIndex.levels previously allowed setting the index name. We # don't allow this anymore, and raise if it happens rather than @@ -541,10 +541,14 @@ def is_(self, other) -> bool: -------- Index.identical : Works like ``Index.is_`` but also checks metadata. """ - try: - return self._id is other._id - except AttributeError: + if self is other: + return True + elif not hasattr(other, "_id"): return False + elif com.any_none(self._id, other._id): + return False + else: + return self._id is other._id def _reset_identity(self) -> None: """