def test_array_shift(self, array: np.ndarray) -> None: for shift in (-1, 1): for wrap in (True, False): tests = [] post1 = util.array_shift(array=array, shift=shift, axis=0, wrap=wrap) tests.append(post1) if array.ndim == 2: post2 = util.array_shift(array=array, shift=shift, axis=1, wrap=wrap) tests.append(post2) for post in tests: self.assertTrue(array.shape == post.shape) # type is only always maintained if we are wrapping if wrap: self.assertTrue(array.dtype == post.dtype)
def roll(self, shift: int, include_index: bool = False) -> 'Series': '''Return a Series with values rotated forward and wrapped around the index (with a postive shift) or backward and wrapped around the index (with a negative shift). Args: shift: Postive or negative integer shift. include_index: Determine if the Index is shifted with the underlying data. ''' if shift % len(self.values): values = array_shift(self.values, shift, axis=0, wrap=True) values.flags.writeable = False else: values = self.values if include_index: index = self._index.roll(shift=shift) own_index = True else: index = self._index own_index = False return self.__class__(values, index=index, name=self._name, own_index=own_index)
def roll(self, shift: int) -> 'Index': '''Return an Index with values rotated forward and wrapped around (with a postive shift) or backward and wrapped around (with a negative shift). ''' values = self.values # force usage of property for cache update if shift % len(values): values = array_shift(values, shift, axis=0, wrap=True) values.flags.writeable = False return self.__class__(values)
def roll(self, shift: int) -> 'IndexHierarchy': '''Return an Index with values rotated forward and wrapped around (with a postive shift) or backward and wrapped around (with a negative shift). ''' if self._recache: self._update_array_cache() values = self._labels if shift % len(values): values = array_shift(array=values, shift=shift, axis=0, wrap=True) values.flags.writeable = False return self.__class__.from_labels(values)
def test_array_shift_b(self): a1 = np.array([('a', 'b', 'e', 'd'), ('c', 'd', 'f', 'w'), ('e', 'f', 's', 'q')]) self.assertEqual( array_shift(a1, 2, axis=0, wrap=True).tolist(), [['c', 'd', 'f', 'w'], ['e', 'f', 's', 'q'], ['a', 'b', 'e', 'd']]) self.assertEqual( array_shift(a1, -2, axis=0, wrap=True).tolist(), [['e', 'f', 's', 'q'], ['a', 'b', 'e', 'd'], ['c', 'd', 'f', 'w']]) self.assertEqual( array_shift(a1, -2, axis=0, wrap=False, fill_value='XX').dtype, np.dtype('<U2')) self.assertEqual( array_shift(a1, -2, axis=0, wrap=False, fill_value='XX').tolist(), [['e', 'f', 's', 'q'], ['XX', 'XX', 'XX', 'XX'], ['XX', 'XX', 'XX', 'XX']]) self.assertEqual( array_shift(a1, 2, axis=1, wrap=False, fill_value='XX').tolist(), [['XX', 'XX', 'a', 'b'], ['XX', 'XX', 'c', 'd'], ['XX', 'XX', 'e', 'f']]) self.assertEqual( array_shift(a1, -2, axis=1, wrap=False, fill_value='XX').tolist(), [['e', 'd', 'XX', 'XX'], ['f', 'w', 'XX', 'XX'], ['s', 'q', 'XX', 'XX']])
def shift(self, shift: int, fill_value=np.nan) -> 'Series': '''Return a Series with values shifted forward on the index (with a postive shift) or backward on the index (with a negative shift). Args: shift: Postive or negative integer shift. fill_value: Value to be used to fill data missing after the shift. ''' if shift: values = array_shift(self.values, shift, axis=0, wrap=False, fill_value=fill_value) values.flags.writeable = False else: values = self.values return self.__class__(values, index=self._index)
def test_array_shift_a(self): a1 = np.arange(6) # import ipdb; ipdb.set_trace() self.assertEqual( array_shift(a1, 2, axis=0, wrap=True).tolist(), [4, 5, 0, 1, 2, 3]) self.assertEqual( array_shift(a1, -2, axis=0, wrap=True).tolist(), [2, 3, 4, 5, 0, 1]) self.assertEqual( array_shift(a1, 5, axis=0, wrap=True).tolist(), [1, 2, 3, 4, 5, 0]) self.assertEqual( array_shift(a1, 2, axis=0, wrap=False, fill_value=-1).tolist(), [-1, -1, 0, 1, 2, 3]) self.assertEqual( array_shift(a1, 2, axis=0, wrap=False, fill_value=1.5).tolist(), [1.5, 1.5, 0, 1, 2, 3]) self.assertEqual( array_shift(a1, -2, axis=0, wrap=False, fill_value=1.5).tolist(), [2, 3, 4, 5, 1.5, 1.5])
def test_array_shift_c(self): a1 = np.arange(6) post = array_shift(a1, 0, axis=0, wrap=False) self.assertEqual(a1.tolist(), post.tolist())