Source code for tesfdmtools.methods.shiftarr

#!/usr/bin/env python3
#

import numpy

[docs]def shiftarr(x,n): ''' shift array number of bins in given direction. At the endpoints, the array is extended with the average of the array at the end Args: * `x` = input array of any type (integer, float) * `n` = number of bins to shift, positive or negative ''' if n == 0: xs=x return xs xs=numpy.empty_like(x) if n > 0: xs[n:]=x[:-n] xs[:n]=numpy.mean(x[:n]) return xs if n < 0: xs[:n]=x[-n:] xs[n:]=numpy.mean(x[n:]) return xs
# ============================================ if __name__ == "__main__": x=numpy.array([0,1,2,3,4,5,4,3,2,1,0],dtype=float) print(x) print(shiftarr(x,2)) print(shiftarr(x,-2)) print(shiftarr(x,0))