Skip to content

Commit

Permalink
CLN: small clean-up of IntervalIndex (pandas-dev#22956)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisvandenbossche authored Oct 3, 2018
1 parent b0f9a10 commit 04ea51d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 47 deletions.
7 changes: 1 addition & 6 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 8 additions & 41 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"""
Expand All @@ -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
Expand Down Expand Up @@ -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 """
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 04ea51d

Please sign in to comment.