Would ``np.roll`` help to simplify this function?