asof
Syntax
asof(X, Y)
Parameters
X must be a vector sorted in ascending order, or an indexed series/matrix whose row index is sorted in ascending order.
Y is a scalar/vector/tuple/matrix/dictionary/table/array vector. If X is an indexed series/matrix, Y must be a vector.
Details
If X is a vector, for each element y in Y, return the index of the last element in X that is no greater than y. If nothing is found, return -1.
If X is an indexed series/matrix, for each element y in Y, search the row index of X for the last index no greater than y, and return the data in the corresponding row. If no row index is found, return NULL.
Return value: If X is a vector, the result is INT and has the same form as Y. If X is an indexed series, the result is an indexed series whose row index is Y. If X is an indexed matrix, the result is an indexed matrix whose row index is Y and column labels are the same as X.
Examples
Example 1. X is a vector.
asof(1..100, 60 200 -10)
// output: [59,99,-1]
0 0 0 1 1 1 1 2 2 3 asof 1
// output: 6
Example 2. X is an indexed series.
index = [2023.05.04, 2023.05.06, 2023.05.10]
s = indexedSeries(index, 1..3)
y = [2023.05.03, 2023.05.04, 2023.05.05, 2023.05.09, 2023.05.12]
asof(s, y)
| label | #0 |
|---|---|
| 2023.05.03 | |
| 2023.05.04 | 1 |
| 2023.05.05 | 1 |
| 2023.05.09 | 2 |
| 2023.05.12 | 3 |
Example 3. X is an indexed matrix.
index = [2023.05.04, 2023.05.06, 2023.05.10]
m = matrix(1..3, 11..13).rename!(index, `A`B).setIndexedMatrix!()
y = [2023.05.03, 2023.05.04, 2023.05.05, 2023.05.09, 2023.05.12]
asof(m, y)
| label | A | B |
|---|---|---|
| 2023.05.03 | ||
| 2023.05.04 | 1 | 11 |
| 2023.05.05 | 1 | 11 |
| 2023.05.09 | 2 | 12 |
| 2023.05.12 | 3 | 13 |
