Skip to content

Instantly share code, notes, and snippets.

@asifr
Created December 3, 2020 16:45
Show Gist options
  • Save asifr/bddadbed1302e5007e2a5b1ccfff6daa to your computer and use it in GitHub Desktop.
Save asifr/bddadbed1302e5007e2a5b1ccfff6daa to your computer and use it in GitHub Desktop.
import numpy as np
def ffill(arr: np.ndarray):
arr = arr.T
mask = np.isnan(arr)
idx = np.where(~mask, np.arange(mask.shape[1]), 0)
np.maximum.accumulate(idx, axis=1, out=idx)
out = arr[np.arange(idx.shape[0])[:, None], idx].T
return out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment