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(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]])