Skip to content

Commit d5fa4f5

Browse files
jbrockmendeljreback
authored andcommitted
CLN: unreachable code in indexes (#30694)
1 parent 61d9a8d commit d5fa4f5

File tree

4 files changed

+13
-31
lines changed

4 files changed

+13
-31
lines changed

pandas/core/indexes/category.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -269,14 +269,12 @@ def _create_categorical(cls, data, dtype=None):
269269
return data
270270

271271
@classmethod
272-
def _simple_new(cls, values, name=None, dtype=None, **kwargs):
272+
def _simple_new(cls, values, name=None, dtype=None):
273273
result = object.__new__(cls)
274274

275275
values = cls._create_categorical(values, dtype=dtype)
276276
result._data = values
277277
result.name = name
278-
for k, v in kwargs.items():
279-
setattr(result, k, v)
280278

281279
result._reset_identity()
282280
result._no_setting_name = False

pandas/core/indexes/datetimelike.py

-6
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,6 @@ def equals(self, other):
164164
# have different timezone
165165
return False
166166

167-
elif is_period_dtype(self):
168-
if not is_period_dtype(other):
169-
return False
170-
if self.freq != other.freq:
171-
return False
172-
173167
return np.array_equal(self.asi8, other.asi8)
174168

175169
def _ensure_localized(

pandas/core/indexes/datetimes.py

+4-12
Original file line numberDiff line numberDiff line change
@@ -387,18 +387,12 @@ def _formatter_func(self):
387387
# --------------------------------------------------------------------
388388
# Set Operation Methods
389389

390-
def _union(self, other, sort):
390+
def _union(self, other: "DatetimeIndex", sort):
391391
if not len(other) or self.equals(other) or not len(self):
392392
return super()._union(other, sort=sort)
393393

394-
if len(other) == 0 or self.equals(other) or len(self) == 0:
395-
return super().union(other, sort=sort)
396-
397-
if not isinstance(other, DatetimeIndex):
398-
try:
399-
other = DatetimeIndex(other)
400-
except TypeError:
401-
pass
394+
# We are called by `union`, which is responsible for this validation
395+
assert isinstance(other, DatetimeIndex)
402396

403397
this, other = self._maybe_utc_convert(other)
404398

@@ -407,9 +401,7 @@ def _union(self, other, sort):
407401
else:
408402
result = Index._union(this, other, sort=sort)
409403
if isinstance(result, DatetimeIndex):
410-
# TODO: we shouldn't be setting attributes like this;
411-
# in all the tests this equality already holds
412-
result._data._dtype = this.dtype
404+
assert result._data.dtype == this.dtype
413405
if result.freq is None and (
414406
this.freq is not None or other.freq is not None
415407
):

pandas/core/indexes/timedeltas.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -248,15 +248,13 @@ def astype(self, dtype, copy=True):
248248
return Index(result.astype("i8"), name=self.name)
249249
return DatetimeIndexOpsMixin.astype(self, dtype, copy=copy)
250250

251-
def _union(self, other, sort):
251+
def _union(self, other: "TimedeltaIndex", sort):
252252
if len(other) == 0 or self.equals(other) or len(self) == 0:
253253
return super()._union(other, sort=sort)
254254

255-
if not isinstance(other, TimedeltaIndex):
256-
try:
257-
other = TimedeltaIndex(other)
258-
except (TypeError, ValueError):
259-
pass
255+
# We are called by `union`, which is responsible for this validation
256+
assert isinstance(other, TimedeltaIndex)
257+
260258
this, other = self, other
261259

262260
if this._can_fast_union(other):
@@ -309,7 +307,7 @@ def get_value(self, series, key):
309307
return self.get_value_maybe_box(series, key)
310308

311309
try:
312-
return com.maybe_box(self, Index.get_value(self, series, key), series, key)
310+
value = Index.get_value(self, series, key)
313311
except KeyError:
314312
try:
315313
loc = self._get_string_slice(key)
@@ -321,10 +319,10 @@ def get_value(self, series, key):
321319
return self.get_value_maybe_box(series, key)
322320
except (TypeError, ValueError, KeyError):
323321
raise KeyError(key)
322+
else:
323+
return com.maybe_box(self, value, series, key)
324324

325-
def get_value_maybe_box(self, series, key):
326-
if not isinstance(key, Timedelta):
327-
key = Timedelta(key)
325+
def get_value_maybe_box(self, series, key: Timedelta):
328326
values = self._engine.get_value(com.values_from_object(series), key)
329327
return com.maybe_box(self, values, series, key)
330328

0 commit comments

Comments
 (0)