Skip to content

Commit e756e99

Browse files
jbrockmendeljorisvandenbossche
authored andcommitted
CLN: Use is_period_dtype instead of ABCPeriodIndex checks (#22958)
1 parent 03181f0 commit e756e99

File tree

4 files changed

+20
-20
lines changed

4 files changed

+20
-20
lines changed

pandas/core/arrays/period.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def asfreq(self, freq=None, how='E'):
264264
if self.hasnans:
265265
new_data[self._isnan] = iNaT
266266

267-
return self._simple_new(new_data, self.name, freq=freq)
267+
return self._shallow_copy(new_data, freq=freq)
268268

269269
# ------------------------------------------------------------------
270270
# Arithmetic Methods

pandas/core/indexes/datetimelike.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@
2121
is_list_like,
2222
is_scalar,
2323
is_bool_dtype,
24+
is_period_dtype,
2425
is_categorical_dtype,
2526
is_datetime_or_timedelta_dtype,
2627
is_float_dtype,
2728
is_integer_dtype,
2829
is_object_dtype,
2930
is_string_dtype)
3031
from pandas.core.dtypes.generic import (
31-
ABCIndex, ABCSeries, ABCPeriodIndex, ABCIndexClass)
32+
ABCIndex, ABCSeries, ABCIndexClass)
3233
from pandas.core.dtypes.missing import isna
3334
from pandas.core import common as com, algorithms, ops
3435

@@ -239,9 +240,8 @@ def equals(self, other):
239240
# have different timezone
240241
return False
241242

242-
# ToDo: Remove this when PeriodDtype is added
243-
elif isinstance(self, ABCPeriodIndex):
244-
if not isinstance(other, ABCPeriodIndex):
243+
elif is_period_dtype(self):
244+
if not is_period_dtype(other):
245245
return False
246246
if self.freq != other.freq:
247247
return False
@@ -359,7 +359,7 @@ def sort_values(self, return_indexer=False, ascending=True):
359359
attribs = self._get_attributes_dict()
360360
freq = attribs['freq']
361361

362-
if freq is not None and not isinstance(self, ABCPeriodIndex):
362+
if freq is not None and not is_period_dtype(self):
363363
if freq.n > 0 and not ascending:
364364
freq = freq * -1
365365
elif freq.n < 0 and ascending:
@@ -386,8 +386,8 @@ def take(self, indices, axis=0, allow_fill=True,
386386
fill_value=fill_value,
387387
na_value=iNaT)
388388

389-
# keep freq in PeriodIndex, reset otherwise
390-
freq = self.freq if isinstance(self, ABCPeriodIndex) else None
389+
# keep freq in PeriodArray/Index, reset otherwise
390+
freq = self.freq if is_period_dtype(self) else None
391391
return self._shallow_copy(taken, freq=freq)
392392

393393
_can_hold_na = True
@@ -618,7 +618,7 @@ def repeat(self, repeats, *args, **kwargs):
618618
Analogous to ndarray.repeat
619619
"""
620620
nv.validate_repeat(args, kwargs)
621-
if isinstance(self, ABCPeriodIndex):
621+
if is_period_dtype(self):
622622
freq = self.freq
623623
else:
624624
freq = None
@@ -673,7 +673,7 @@ def _concat_same_dtype(self, to_concat, name):
673673
attribs = self._get_attributes_dict()
674674
attribs['name'] = name
675675

676-
if not isinstance(self, ABCPeriodIndex):
676+
if not is_period_dtype(self):
677677
# reset freq
678678
attribs['freq'] = None
679679

pandas/core/indexes/multi.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ def _try_mi(k):
10011001
(compat.PY3 and isinstance(key, compat.string_types))):
10021002
try:
10031003
return _try_mi(key)
1004-
except (KeyError):
1004+
except KeyError:
10051005
raise
10061006
except (IndexError, ValueError, TypeError):
10071007
pass

pandas/core/indexes/range.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -512,33 +512,33 @@ def __getitem__(self, key):
512512
# This is basically PySlice_GetIndicesEx, but delegation to our
513513
# super routines if we don't have integers
514514

515-
l = len(self)
515+
length = len(self)
516516

517517
# complete missing slice information
518518
step = 1 if key.step is None else key.step
519519
if key.start is None:
520-
start = l - 1 if step < 0 else 0
520+
start = length - 1 if step < 0 else 0
521521
else:
522522
start = key.start
523523

524524
if start < 0:
525-
start += l
525+
start += length
526526
if start < 0:
527527
start = -1 if step < 0 else 0
528-
if start >= l:
529-
start = l - 1 if step < 0 else l
528+
if start >= length:
529+
start = length - 1 if step < 0 else length
530530

531531
if key.stop is None:
532-
stop = -1 if step < 0 else l
532+
stop = -1 if step < 0 else length
533533
else:
534534
stop = key.stop
535535

536536
if stop < 0:
537-
stop += l
537+
stop += length
538538
if stop < 0:
539539
stop = -1
540-
if stop > l:
541-
stop = l
540+
if stop > length:
541+
stop = length
542542

543543
# delegate non-integer slices
544544
if (start != int(start) or

0 commit comments

Comments
 (0)