示例#1
0
    def test_isin(self, array: np.ndarray) -> None:

        container_factory = (list, set, np.array)
        result = None

        if array.ndim == 1:
            sample = array[0]
            if np.array(sample).dtype.kind in DTYPE_INEXACT_KINDS and np.isnan(
                    sample):
                pass
            else:
                for factory in container_factory:
                    result = util.isin(array, factory((sample, )))
                    self.assertTrue(result[0])
        elif array.ndim == 2:
            sample = array[0, 0]
            if np.array(sample).dtype.kind in DTYPE_INEXACT_KINDS and np.isnan(
                    sample):
                pass
            else:
                for factory in container_factory:
                    result = util.isin(array, factory((sample, )))
                    self.assertTrue(result[0, 0])

        if result is not None:
            self.assertTrue(array.shape == result.shape)
            self.assertTrue(result.dtype == bool)
示例#2
0
    def isin(self, other: tp.Iterable[tp.Iterable[tp.Hashable]]) -> np.ndarray:
        '''
        Return a Boolean array showing True where one or more of the passed in iterable of labels is found in the index.
        '''
        if self._recache:
            self._update_array_cache()

        matches = []
        for seq in other:
            if not hasattr(seq, '__iter__'):
                raise RuntimeError(
                    'must provide one or more iterables within an iterable')
            # Coerce to hashable type
            as_tuple = tuple(seq)
            if len(as_tuple) == self.depth:
                # can pre-filter if iterable matches to length
                matches.append(as_tuple)

        if not matches:
            return np.full(self._length, False, dtype=bool)

        return isin(self.flat().values, matches)
示例#3
0
 def isin(self, other: tp.Iterable[tp.Any]) -> np.ndarray:
     '''
     Return a Boolean array showing True where a label is found in other. If other is a multidimensional array, it is flattened.
     '''
     return isin(self.values, other, array_is_unique=True)