예제 #1
0
    def test_maybe_indices_to_slice_middle(self):
        target = np.arange(100)

        # slice
        for start, end in [(2, 10), (5, 25), (65, 97)]:
            for step in [1, 2, 4, 20]:
                indices = np.arange(start, end, step, dtype=np.int64)
                maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
                self.assertTrue(isinstance(maybe_slice, slice))
                self.assert_numpy_array_equal(target[indices],
                                              target[maybe_slice])

                # reverse
                indices = indices[::-1]
                maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
                self.assertTrue(isinstance(maybe_slice, slice))
                self.assert_numpy_array_equal(target[indices],
                                              target[maybe_slice])

        # not slice
        for case in [[14, 12, 10, 12], [12, 12, 11, 10], [10, 11, 12, 11]]:
            indices = np.array(case, dtype=np.int64)
            maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
            self.assertFalse(isinstance(maybe_slice, slice))
            self.assert_numpy_array_equal(maybe_slice, indices)
            self.assert_numpy_array_equal(target[indices], target[maybe_slice])
예제 #2
0
    def test_maybe_indices_to_slice_middle(self):
        target = np.arange(100)

        # slice
        for start, end in [(2, 10), (5, 25), (65, 97)]:
            for step in [1, 2, 4, 20]:
                indices = np.arange(start, end, step, dtype=np.int64)
                maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
                self.assertTrue(isinstance(maybe_slice, slice))
                self.assert_numpy_array_equal(target[indices],
                                              target[maybe_slice])

                # reverse
                indices = indices[::-1]
                maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
                self.assertTrue(isinstance(maybe_slice, slice))
                self.assert_numpy_array_equal(target[indices],
                                              target[maybe_slice])

        # not slice
        for case in [[14, 12, 10, 12], [12, 12, 11, 10], [10, 11, 12, 11]]:
            indices = np.array(case, dtype=np.int64)
            maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
            self.assertFalse(isinstance(maybe_slice, slice))
            self.assert_numpy_array_equal(maybe_slice, indices)
            self.assert_numpy_array_equal(target[indices], target[maybe_slice])
예제 #3
0
    def test_maybe_indices_to_slice_left_edge(self):
        target = np.arange(100)

        # slice
        indices = np.array([], dtype=np.int64)
        maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
        self.assertTrue(isinstance(maybe_slice, slice))
        self.assert_numpy_array_equal(target[indices], target[maybe_slice])

        for end in [1, 2, 5, 20, 99]:
            for step in [1, 2, 4]:
                indices = np.arange(0, end, step, dtype=np.int64)
                maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
                self.assertTrue(isinstance(maybe_slice, slice))
                self.assert_numpy_array_equal(target[indices],
                                              target[maybe_slice])

                # reverse
                indices = indices[::-1]
                maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
                self.assertTrue(isinstance(maybe_slice, slice))
                self.assert_numpy_array_equal(target[indices],
                                              target[maybe_slice])

        # not slice
        for case in [[2, 1, 2, 0], [2, 2, 1, 0], [0, 1, 2, 1], [-2, 0, 2],
                     [2, 0, -2]]:
            indices = np.array(case, dtype=np.int64)
            maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
            self.assertFalse(isinstance(maybe_slice, slice))
            self.assert_numpy_array_equal(maybe_slice, indices)
            self.assert_numpy_array_equal(target[indices], target[maybe_slice])
예제 #4
0
    def test_maybe_indices_to_slice_left_edge(self):
        target = np.arange(100)

        # slice
        indices = np.array([], dtype=np.int64)
        maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
        self.assertTrue(isinstance(maybe_slice, slice))
        self.assert_numpy_array_equal(target[indices], target[maybe_slice])

        for end in [1, 2, 5, 20, 99]:
            for step in [1, 2, 4]:
                indices = np.arange(0, end, step, dtype=np.int64)
                maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
                self.assertTrue(isinstance(maybe_slice, slice))
                self.assert_numpy_array_equal(target[indices],
                                              target[maybe_slice])

                # reverse
                indices = indices[::-1]
                maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
                self.assertTrue(isinstance(maybe_slice, slice))
                self.assert_numpy_array_equal(target[indices],
                                              target[maybe_slice])

        # not slice
        for case in [[2, 1, 2, 0], [2, 2, 1, 0], [0, 1, 2, 1], [-2, 0, 2],
                     [2, 0, -2]]:
            indices = np.array(case, dtype=np.int64)
            maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
            self.assertFalse(isinstance(maybe_slice, slice))
            self.assert_numpy_array_equal(maybe_slice, indices)
            self.assert_numpy_array_equal(target[indices], target[maybe_slice])
예제 #5
0
파일: tdi.py 프로젝트: AbnerZheng/pandas
    def delete(self, loc):
        """
        Make a new DatetimeIndex with passed location(s) deleted.

        Parameters
        ----------
        loc: int, slice or array of ints
            Indicate which sub-arrays to remove.

        Returns
        -------
        new_index : TimedeltaIndex
        """
        new_tds = np.delete(self.asi8, loc)

        freq = 'infer'
        if is_integer(loc):
            if loc in (0, -len(self), -1, len(self) - 1):
                freq = self.freq
        else:
            if com.is_list_like(loc):
                loc = lib.maybe_indices_to_slice(
                    com._ensure_int64(np.array(loc)), len(self))
            if isinstance(loc, slice) and loc.step in (1, None):
                if (loc.start in (0, None) or loc.stop in (len(self), None)):
                    freq = self.freq

        return TimedeltaIndex(new_tds, name=self.name, freq=freq)
예제 #6
0
    def delete(self, loc):
        """
        Make a new DatetimeIndex with passed location(s) deleted.

        Parameters
        ----------
        loc: int, slice or array of ints
            Indicate which sub-arrays to remove.

        Returns
        -------
        new_index : TimedeltaIndex
        """
        new_tds = np.delete(self.asi8, loc)

        freq = 'infer'
        if is_integer(loc):
            if loc in (0, -len(self), -1, len(self) - 1):
                freq = self.freq
        else:
            if com.is_list_like(loc):
                loc = lib.maybe_indices_to_slice(
                    com._ensure_int64(np.array(loc)), len(self))
            if isinstance(loc, slice) and loc.step in (1, None):
                if (loc.start in (0, None) or loc.stop in (len(self), None)):
                    freq = self.freq

        return TimedeltaIndex(new_tds, name=self.name, freq=freq)
예제 #7
0
 def take(self, indices, axis=0):
     """
     Analogous to ndarray.take
     """
     maybe_slice = lib.maybe_indices_to_slice(com._ensure_int64(indices))
     if isinstance(maybe_slice, slice):
         return self[maybe_slice]
     return super(DatetimeIndexOpsMixin, self).take(indices, axis)
예제 #8
0
 def take(self, indices, axis=0):
     """
     Analogous to ndarray.take
     """
     maybe_slice = lib.maybe_indices_to_slice(com._ensure_int64(indices))
     if isinstance(maybe_slice, slice):
         return self[maybe_slice]
     return super(DatetimeIndexOpsMixin, self).take(indices, axis)
예제 #9
0
    def test_maybe_indices_to_slice_right_edge(self):
        target = np.arange(100)

        # slice
        for start in [0, 2, 5, 20, 97, 98]:
            for step in [1, 2, 4]:
                indices = np.arange(start, 99, step, dtype=np.int64)
                maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
                self.assertTrue(isinstance(maybe_slice, slice))
                self.assert_numpy_array_equal(target[indices],
                                              target[maybe_slice])

                # reverse
                indices = indices[::-1]
                maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
                self.assertTrue(isinstance(maybe_slice, slice))
                self.assert_numpy_array_equal(target[indices],
                                              target[maybe_slice])

        # not slice
        indices = np.array([97, 98, 99, 100], dtype=np.int64)
        maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
        self.assertFalse(isinstance(maybe_slice, slice))
        self.assert_numpy_array_equal(maybe_slice, indices)
        with self.assertRaises(IndexError):
            target[indices]
        with self.assertRaises(IndexError):
            target[maybe_slice]

        indices = np.array([100, 99, 98, 97], dtype=np.int64)
        maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
        self.assertFalse(isinstance(maybe_slice, slice))
        self.assert_numpy_array_equal(maybe_slice, indices)
        with self.assertRaises(IndexError):
            target[indices]
        with self.assertRaises(IndexError):
            target[maybe_slice]

        for case in [[99, 97, 99, 96], [99, 99, 98, 97], [98, 98, 97, 96]]:
            indices = np.array(case, dtype=np.int64)
            maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
            self.assertFalse(isinstance(maybe_slice, slice))
            self.assert_numpy_array_equal(maybe_slice, indices)
            self.assert_numpy_array_equal(target[indices], target[maybe_slice])
예제 #10
0
    def test_maybe_indices_to_slice_right_edge(self):
        target = np.arange(100)

        # slice
        for start in [0, 2, 5, 20, 97, 98]:
            for step in [1, 2, 4]:
                indices = np.arange(start, 99, step, dtype=np.int64)
                maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
                self.assertTrue(isinstance(maybe_slice, slice))
                self.assert_numpy_array_equal(target[indices],
                                              target[maybe_slice])

                # reverse
                indices = indices[::-1]
                maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
                self.assertTrue(isinstance(maybe_slice, slice))
                self.assert_numpy_array_equal(target[indices],
                                              target[maybe_slice])

        # not slice
        indices = np.array([97, 98, 99, 100], dtype=np.int64)
        maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
        self.assertFalse(isinstance(maybe_slice, slice))
        self.assert_numpy_array_equal(maybe_slice, indices)
        with self.assertRaises(IndexError):
            target[indices]
        with self.assertRaises(IndexError):
            target[maybe_slice]

        indices = np.array([100, 99, 98, 97], dtype=np.int64)
        maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
        self.assertFalse(isinstance(maybe_slice, slice))
        self.assert_numpy_array_equal(maybe_slice, indices)
        with self.assertRaises(IndexError):
            target[indices]
        with self.assertRaises(IndexError):
            target[maybe_slice]

        for case in [[99, 97, 99, 96], [99, 99, 98, 97], [98, 98, 97, 96]]:
            indices = np.array(case, dtype=np.int64)
            maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
            self.assertFalse(isinstance(maybe_slice, slice))
            self.assert_numpy_array_equal(maybe_slice, indices)
            self.assert_numpy_array_equal(target[indices], target[maybe_slice])
예제 #11
0
파일: index.py 프로젝트: zkluo1/pandas
 def take(self, indices, axis=0):
     """
     Analogous to ndarray.take
     """
     maybe_slice = lib.maybe_indices_to_slice(com._ensure_int64(indices))
     if isinstance(maybe_slice, slice):
         return self[maybe_slice]
     indices = com._ensure_platform_int(indices)
     taken = self.values.take(indices, axis=axis)
     return DatetimeIndex(taken, tz=self.tz, name=self.name)
예제 #12
0
 def take(self, indices, axis=0):
     """
     Analogous to ndarray.take
     """
     maybe_slice = lib.maybe_indices_to_slice(com._ensure_int64(indices))
     if isinstance(maybe_slice, slice):
         return self[maybe_slice]
     indices = com._ensure_platform_int(indices)
     taken = self.values.take(indices, axis=axis)
     return self._simple_new(taken, self.name, None, self.tz)
예제 #13
0
 def take(self, indices, axis=0):
     """
     Analogous to ndarray.take
     """
     indices = com._ensure_int64(indices)
     maybe_slice = lib.maybe_indices_to_slice(indices, len(self))
     if isinstance(maybe_slice, slice):
         return self[maybe_slice]
     taken = self.asi8.take(com._ensure_platform_int(indices))
     return self._shallow_copy(taken, freq=None)
예제 #14
0
파일: index.py 프로젝트: afonit/pandas
 def take(self, indices, axis=0):
     """
     Analogous to ndarray.take
     """
     maybe_slice = lib.maybe_indices_to_slice(com._ensure_int64(indices))
     if isinstance(maybe_slice, slice):
         return self[maybe_slice]
     indices = com._ensure_platform_int(indices)
     taken = self.values.take(indices, axis=axis)
     return DatetimeIndex(taken, tz=self.tz, name=self.name)
예제 #15
0
파일: index.py 프로젝트: joaonatali/pandas
 def take(self, indices, axis=0):
     """
     Analogous to ndarray.take
     """
     maybe_slice = lib.maybe_indices_to_slice(com._ensure_int64(indices))
     if isinstance(maybe_slice, slice):
         return self[maybe_slice]
     indices = com._ensure_platform_int(indices)
     taken = self.values.take(indices, axis=axis)
     return self._simple_new(taken, self.name, None, self.tz)
예제 #16
0
파일: base.py 프로젝트: bjacobowski/pandas
 def take(self, indices, axis=0, **kwargs):
     """
     Analogous to ndarray.take
     """
     indices = com._ensure_int64(indices)
     maybe_slice = lib.maybe_indices_to_slice(indices, len(self))
     if isinstance(maybe_slice, slice):
         return self[maybe_slice]
     taken = self.asi8.take(com._ensure_platform_int(indices))
     return self._shallow_copy(taken, freq=None)
예제 #17
0
    def test_maybe_indices_to_slice_both_edges(self):
        target = np.arange(10)

        # slice
        for step in [1, 2, 4, 5, 8, 9]:
            indices = np.arange(0, 9, step, dtype=np.int64)
            maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
            self.assertTrue(isinstance(maybe_slice, slice))
            self.assert_numpy_array_equal(target[indices], target[maybe_slice])

            # reverse
            indices = indices[::-1]
            maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
            self.assertTrue(isinstance(maybe_slice, slice))
            self.assert_numpy_array_equal(target[indices], target[maybe_slice])

        # not slice
        for case in [[4, 2, 0, -2], [2, 2, 1, 0], [0, 1, 2, 1]]:
            indices = np.array(case, dtype=np.int64)
            maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
            self.assertFalse(isinstance(maybe_slice, slice))
            self.assert_numpy_array_equal(maybe_slice, indices)
            self.assert_numpy_array_equal(target[indices], target[maybe_slice])
예제 #18
0
    def test_maybe_indices_to_slice_both_edges(self):
        target = np.arange(10)

        # slice
        for step in [1, 2, 4, 5, 8, 9]:
            indices = np.arange(0, 9, step, dtype=np.int64)
            maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
            self.assertTrue(isinstance(maybe_slice, slice))
            self.assert_numpy_array_equal(target[indices], target[maybe_slice])

            # reverse
            indices = indices[::-1]
            maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
            self.assertTrue(isinstance(maybe_slice, slice))
            self.assert_numpy_array_equal(target[indices], target[maybe_slice])

        # not slice
        for case in [[4, 2, 0, -2], [2, 2, 1, 0], [0, 1, 2, 1]]:
            indices = np.array(case, dtype=np.int64)
            maybe_slice = lib.maybe_indices_to_slice(indices, len(target))
            self.assertFalse(isinstance(maybe_slice, slice))
            self.assert_numpy_array_equal(maybe_slice, indices)
            self.assert_numpy_array_equal(target[indices], target[maybe_slice])
예제 #19
0
파일: base.py 프로젝트: Allen1203/pandas
    def take(self, indices, axis=0, allow_fill=True, fill_value=None):
        indices = com._ensure_int64(indices)

        maybe_slice = lib.maybe_indices_to_slice(indices, len(self))
        if isinstance(maybe_slice, slice):
            return self[maybe_slice]

        taken = self._assert_take_fillable(self.asi8, indices,
                                           allow_fill=allow_fill,
                                           fill_value=fill_value,
                                           na_value=tslib.iNaT)

        # keep freq in PeriodIndex, reset otherwise
        freq = self.freq if isinstance(self, com.ABCPeriodIndex) else None
        return self._shallow_copy(taken, freq=freq)
예제 #20
0
파일: base.py 프로젝트: yaduart/pandas
    def take(self, indices, axis=0, allow_fill=True, fill_value=None):
        indices = com._ensure_int64(indices)

        maybe_slice = lib.maybe_indices_to_slice(indices, len(self))
        if isinstance(maybe_slice, slice):
            return self[maybe_slice]

        taken = self._assert_take_fillable(self.asi8, indices,
                                           allow_fill=allow_fill,
                                           fill_value=fill_value,
                                           na_value=tslib.iNaT)

        # keep freq in PeriodIndex, reset otherwise
        freq = self.freq if isinstance(self, com.ABCPeriodIndex) else None
        return self._shallow_copy(taken, freq=freq)
예제 #21
0
파일: base.py 프로젝트: sxwang/pandas
    def take(self, indices, axis=0, allow_fill=True, fill_value=None):
        """
        Analogous to ndarray.take
        """
        indices = com._ensure_int64(indices)
        maybe_slice = lib.maybe_indices_to_slice(indices, len(self))
        if isinstance(maybe_slice, slice):
            return self[maybe_slice]
        taken = self.asi8.take(com._ensure_platform_int(indices))

        # only fill if we are passing a non-None fill_value
        if allow_fill and fill_value is not None:
            mask = indices == -1
            if mask.any():
                taken[mask] = tslib.iNaT
        return self._shallow_copy(taken, freq=None)
예제 #22
0
    def take(self, indices, axis=0, allow_fill=True, fill_value=None):
        """
        Analogous to ndarray.take
        """
        indices = com._ensure_int64(indices)
        maybe_slice = lib.maybe_indices_to_slice(indices, len(self))
        if isinstance(maybe_slice, slice):
            return self[maybe_slice]
        taken = self.asi8.take(com._ensure_platform_int(indices))

        # only fill if we are passing a non-None fill_value
        if allow_fill and fill_value is not None:
            mask = indices == -1
            if mask.any():
                taken[mask] = tslib.iNaT
        return self._shallow_copy(taken, freq=None)