np_shift

pyhelpers.ops.np_shift(array, step, fill_value=nan)

Shift an array by desired number of rows.

See also [OPS-NS-1]

Parameters
  • array (numpy.ndarray) – an array of numbers

  • step (int) – number of rows to shift

  • fill_value (float or int) – values to fill missing rows due to the shift, defaults to NaN

Returns

shifted array

Return type

numpy.ndarray

Examples:

>>> from pyhelpers.ops import np_shift
>>> from pyhelpers._cache import example_dataframe

>>> arr = example_dataframe().to_numpy()
>>> arr
array([[530034, 180381],
       [406689, 286822],
       [383819, 398052],
       [582044, 152953]], dtype=int64)

>>> np_shift(arr, step=-1)
array([[406689., 286822.],
       [383819., 398052.],
       [582044., 152953.],
       [    nan,     nan]])

>>> np_shift(arr, step=1, fill_value=0)
array([[     0,      0],
       [530034, 180381],
       [406689, 286822],
       [383819, 398052]], dtype=int64)