diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index 8b9e56b39f75f..b44b83cec7b71 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -269,14 +269,12 @@ def _create_categorical(cls, data, dtype=None): return data @classmethod - def _simple_new(cls, values, name=None, dtype=None, **kwargs): + def _simple_new(cls, values, name=None, dtype=None): result = object.__new__(cls) values = cls._create_categorical(values, dtype=dtype) result._data = values result.name = name - for k, v in kwargs.items(): - setattr(result, k, v) result._reset_identity() result._no_setting_name = False diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index de27f0c0be850..f1b6f5c6d43b4 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -164,12 +164,6 @@ def equals(self, other): # have different timezone return False - elif is_period_dtype(self): - if not is_period_dtype(other): - return False - if self.freq != other.freq: - return False - return np.array_equal(self.asi8, other.asi8) def _ensure_localized( diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index ec95b6c483c52..64bbb2a53725f 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -387,18 +387,12 @@ def _formatter_func(self): # -------------------------------------------------------------------- # Set Operation Methods - def _union(self, other, sort): + def _union(self, other: "DatetimeIndex", sort): if not len(other) or self.equals(other) or not len(self): return super()._union(other, sort=sort) - if len(other) == 0 or self.equals(other) or len(self) == 0: - return super().union(other, sort=sort) - - if not isinstance(other, DatetimeIndex): - try: - other = DatetimeIndex(other) - except TypeError: - pass + # We are called by `union`, which is responsible for this validation + assert isinstance(other, DatetimeIndex) this, other = self._maybe_utc_convert(other) @@ -407,9 +401,7 @@ def _union(self, other, sort): else: result = Index._union(this, other, sort=sort) if isinstance(result, DatetimeIndex): - # TODO: we shouldn't be setting attributes like this; - # in all the tests this equality already holds - result._data._dtype = this.dtype + assert result._data.dtype == this.dtype if result.freq is None and ( this.freq is not None or other.freq is not None ): diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index 6fa7a7d3d4fb6..6cb4410d6a305 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -248,15 +248,13 @@ def astype(self, dtype, copy=True): return Index(result.astype("i8"), name=self.name) return DatetimeIndexOpsMixin.astype(self, dtype, copy=copy) - def _union(self, other, sort): + def _union(self, other: "TimedeltaIndex", sort): if len(other) == 0 or self.equals(other) or len(self) == 0: return super()._union(other, sort=sort) - if not isinstance(other, TimedeltaIndex): - try: - other = TimedeltaIndex(other) - except (TypeError, ValueError): - pass + # We are called by `union`, which is responsible for this validation + assert isinstance(other, TimedeltaIndex) + this, other = self, other if this._can_fast_union(other): @@ -309,7 +307,7 @@ def get_value(self, series, key): return self.get_value_maybe_box(series, key) try: - return com.maybe_box(self, Index.get_value(self, series, key), series, key) + value = Index.get_value(self, series, key) except KeyError: try: loc = self._get_string_slice(key) @@ -321,10 +319,10 @@ def get_value(self, series, key): return self.get_value_maybe_box(series, key) except (TypeError, ValueError, KeyError): raise KeyError(key) + else: + return com.maybe_box(self, value, series, key) - def get_value_maybe_box(self, series, key): - if not isinstance(key, Timedelta): - key = Timedelta(key) + def get_value_maybe_box(self, series, key: Timedelta): values = self._engine.get_value(com.values_from_object(series), key) return com.maybe_box(self, values, series, key)