np_shift

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

Shift an array by a desired number of rows.

See also [OPS-NS-1].

Parameters:
  • array (numpy.ndarray) – An array of numbers.

  • step (int) – Number of rows to shift. Positive value shifts downwards; negative shifts upwards.

  • fill_value (float | int) – Value 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(osgb36=True).to_numpy()
>>> arr
array([[530039.5588445, 180371.6801655],
       [406705.8870136, 286868.1666422],
       [383830.0390357, 398113.0558309],
       [430147.4473539, 433553.3271173]])
>>> np_shift(arr, step=-1)
array([[406705.8870136, 286868.1666422],
       [383830.0390357, 398113.0558309],
       [430147.4473539, 433553.3271173],
       [           nan,            nan]])
>>> np_shift(arr, step=1, fill_value=0)
array([[     0,      0],
       [530039, 180371],
       [406705, 286868],
       [383830, 398113]])