From e756e991d57c2656906d0a3e8fc76950844e3f3e Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Wed, 3 Oct 2018 02:19:27 -0700 Subject: [PATCH] CLN: Use is_period_dtype instead of ABCPeriodIndex checks (#22958) --- pandas/core/arrays/period.py | 2 +- pandas/core/indexes/datetimelike.py | 18 +++++++++--------- pandas/core/indexes/multi.py | 2 +- pandas/core/indexes/range.py | 18 +++++++++--------- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 481d5313f0e25..41b4c5c669efc 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -264,7 +264,7 @@ def asfreq(self, freq=None, how='E'): if self.hasnans: new_data[self._isnan] = iNaT - return self._simple_new(new_data, self.name, freq=freq) + return self._shallow_copy(new_data, freq=freq) # ------------------------------------------------------------------ # Arithmetic Methods diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 37a12a588db03..1ec30ecbb3a3b 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -21,6 +21,7 @@ is_list_like, is_scalar, is_bool_dtype, + is_period_dtype, is_categorical_dtype, is_datetime_or_timedelta_dtype, is_float_dtype, @@ -28,7 +29,7 @@ is_object_dtype, is_string_dtype) from pandas.core.dtypes.generic import ( - ABCIndex, ABCSeries, ABCPeriodIndex, ABCIndexClass) + ABCIndex, ABCSeries, ABCIndexClass) from pandas.core.dtypes.missing import isna from pandas.core import common as com, algorithms, ops @@ -239,9 +240,8 @@ def equals(self, other): # have different timezone return False - # ToDo: Remove this when PeriodDtype is added - elif isinstance(self, ABCPeriodIndex): - if not isinstance(other, ABCPeriodIndex): + elif is_period_dtype(self): + if not is_period_dtype(other): return False if self.freq != other.freq: return False @@ -359,7 +359,7 @@ def sort_values(self, return_indexer=False, ascending=True): attribs = self._get_attributes_dict() freq = attribs['freq'] - if freq is not None and not isinstance(self, ABCPeriodIndex): + if freq is not None and not is_period_dtype(self): if freq.n > 0 and not ascending: freq = freq * -1 elif freq.n < 0 and ascending: @@ -386,8 +386,8 @@ def take(self, indices, axis=0, allow_fill=True, fill_value=fill_value, na_value=iNaT) - # keep freq in PeriodIndex, reset otherwise - freq = self.freq if isinstance(self, ABCPeriodIndex) else None + # keep freq in PeriodArray/Index, reset otherwise + freq = self.freq if is_period_dtype(self) else None return self._shallow_copy(taken, freq=freq) _can_hold_na = True @@ -618,7 +618,7 @@ def repeat(self, repeats, *args, **kwargs): Analogous to ndarray.repeat """ nv.validate_repeat(args, kwargs) - if isinstance(self, ABCPeriodIndex): + if is_period_dtype(self): freq = self.freq else: freq = None @@ -673,7 +673,7 @@ def _concat_same_dtype(self, to_concat, name): attribs = self._get_attributes_dict() attribs['name'] = name - if not isinstance(self, ABCPeriodIndex): + if not is_period_dtype(self): # reset freq attribs['freq'] = None diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index 119a607fc0e68..6091df776a01b 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -1001,7 +1001,7 @@ def _try_mi(k): (compat.PY3 and isinstance(key, compat.string_types))): try: return _try_mi(key) - except (KeyError): + except KeyError: raise except (IndexError, ValueError, TypeError): pass diff --git a/pandas/core/indexes/range.py b/pandas/core/indexes/range.py index 981bfddeadac1..fd8e17c369f5a 100644 --- a/pandas/core/indexes/range.py +++ b/pandas/core/indexes/range.py @@ -512,33 +512,33 @@ def __getitem__(self, key): # This is basically PySlice_GetIndicesEx, but delegation to our # super routines if we don't have integers - l = len(self) + length = len(self) # complete missing slice information step = 1 if key.step is None else key.step if key.start is None: - start = l - 1 if step < 0 else 0 + start = length - 1 if step < 0 else 0 else: start = key.start if start < 0: - start += l + start += length if start < 0: start = -1 if step < 0 else 0 - if start >= l: - start = l - 1 if step < 0 else l + if start >= length: + start = length - 1 if step < 0 else length if key.stop is None: - stop = -1 if step < 0 else l + stop = -1 if step < 0 else length else: stop = key.stop if stop < 0: - stop += l + stop += length if stop < 0: stop = -1 - if stop > l: - stop = l + if stop > length: + stop = length # delegate non-integer slices if (start != int(start) or