zmc
2023-08-08 e792e9a60d958b93aef96050644f369feb25d61b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""
transforms.py is for shape-preserving functions.
"""
 
from __future__ import annotations
 
import numpy as np
 
from pandas._typing import AxisInt
 
 
def shift(values: np.ndarray, periods: int, axis: AxisInt, fill_value) -> np.ndarray:
    new_values = values
 
    if periods == 0 or values.size == 0:
        return new_values.copy()
 
    # make sure array sent to np.roll is c_contiguous
    f_ordered = values.flags.f_contiguous
    if f_ordered:
        new_values = new_values.T
        axis = new_values.ndim - axis - 1
 
    if new_values.size:
        new_values = np.roll(
            new_values,
            np.intp(periods),
            axis=axis,
        )
 
    axis_indexer = [slice(None)] * values.ndim
    if periods > 0:
        axis_indexer[axis] = slice(None, periods)
    else:
        axis_indexer[axis] = slice(periods, None)
    new_values[tuple(axis_indexer)] = fill_value
 
    # restore original order
    if f_ordered:
        new_values = new_values.T
 
    return new_values