Skip to content

Instantly share code, notes, and snippets.

@breuderink
Created May 21, 2020 18:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save breuderink/00221a2d575e6749ea08e05227a3af88 to your computer and use it in GitHub Desktop.
Save breuderink/00221a2d575e6749ea08e05227a3af88 to your computer and use it in GitHub Desktop.
Filling missing values with lagged observations
import numpy as np
def lag(x, n=0):
observed = np.isnan(x) == False
observed[0] = True
offsets = np.flatnonzero(observed)
return offsets[np.maximum(np.cumsum(observed) - 1 - n, 0)]
if __name__ == '__main__':
x = np.nan * np.ones(10)
x[2] = 3
x[3] = 5
x[5] = 7
print(x)
for l in range(3):
ii = lag(x, l)
print(ii, x[ii])
@breuderink
Copy link
Author

% python3 -i lag.py
[nan nan  3.  5. nan  7. nan nan nan nan]
[0 0 2 3 3 5 5 5 5 5] [nan nan  3.  5.  5.  7.  7.  7.  7.  7.]
[0 0 0 2 2 3 3 3 3 3] [nan nan nan  3.  3.  5.  5.  5.  5.  5.]
[0 0 0 0 0 2 2 2 2 2] [nan nan nan nan nan  3.  3.  3.  3.  3.]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment