From 04ea51ddf7623b897aaaf2e504952d3c11e88205 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Wed, 3 Oct 2018 09:24:36 +0200 Subject: [PATCH] CLN: small clean-up of IntervalIndex (#22956) --- pandas/core/arrays/interval.py | 7 +---- pandas/core/indexes/interval.py | 49 ++++++--------------------------- 2 files changed, 9 insertions(+), 47 deletions(-) diff --git a/pandas/core/arrays/interval.py b/pandas/core/arrays/interval.py index 90df596b98296..134999f05364f 100644 --- a/pandas/core/arrays/interval.py +++ b/pandas/core/arrays/interval.py @@ -108,12 +108,7 @@ class IntervalArray(IntervalMixin, ExtensionArray): _na_value = _fill_value = np.nan def __new__(cls, data, closed=None, dtype=None, copy=False, - fastpath=False, verify_integrity=True): - - if fastpath: - return cls._simple_new(data.left, data.right, closed, - copy=copy, dtype=dtype, - verify_integrity=False) + verify_integrity=True): if isinstance(data, ABCSeries) and is_interval_dtype(data): data = data.values diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index 4b125580bd7e0..f72f87aeb2af6 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -146,17 +146,13 @@ class IntervalIndex(IntervalMixin, Index): _mask = None def __new__(cls, data, closed=None, dtype=None, copy=False, - name=None, fastpath=False, verify_integrity=True): - - if fastpath: - return cls._simple_new(data, name) + name=None, verify_integrity=True): if name is None and hasattr(data, 'name'): name = data.name with rewrite_exception("IntervalArray", cls.__name__): array = IntervalArray(data, closed=closed, copy=copy, dtype=dtype, - fastpath=fastpath, verify_integrity=verify_integrity) return cls._simple_new(array, name) @@ -187,14 +183,6 @@ def _shallow_copy(self, left=None, right=None, **kwargs): attributes.update(kwargs) return self._simple_new(result, **attributes) - @cache_readonly - def hasnans(self): - """ - Return if the IntervalIndex has any nans; enables various performance - speedups - """ - return self._isnan.any() - @cache_readonly def _isnan(self): """Return a mask indicating if each value is NA""" @@ -206,10 +194,6 @@ def _isnan(self): def _engine(self): return IntervalTree(self.left, self.right, closed=self.closed) - @property - def _constructor(self): - return type(self) - def __contains__(self, key): """ return a boolean if this key is IN the index @@ -394,18 +378,7 @@ def _values(self): @cache_readonly def _ndarray_values(self): - left = self.left - right = self.right - mask = self._isnan - closed = self.closed - - result = np.empty(len(left), dtype=object) - for i in range(len(left)): - if mask[i]: - result[i] = np.nan - else: - result[i] = Interval(left[i], right[i], closed) - return result + return np.array(self._data) def __array__(self, result=None): """ the array interface, return my values """ @@ -892,18 +865,12 @@ def take(self, indices, axis=0, allow_fill=True, return self._simple_new(result, **attributes) def __getitem__(self, value): - mask = self._isnan[value] - if is_scalar(mask) and mask: - return self._na_value - - left = self.left[value] - right = self.right[value] - - # scalar - if not isinstance(left, Index): - return Interval(left, right, self.closed) - - return self._shallow_copy(left, right) + result = self._data[value] + if isinstance(result, IntervalArray): + return self._shallow_copy(result) + else: + # scalar + return result # __repr__ associated methods are based on MultiIndex