Description
Hi,
In R, there is a way to perform a rolling join as shown below.
library("data.table")
DT <-
data.table(
x = rep(c("a", "b", "c"), each = 3),
y = c(1, 3, 6),
v = 1:9)
setkey(DT, x, y) # Necessary for following self join.
DT[J("a", 4:6)]
DT[J("a", 4:6), roll = TRUE]
The relevant output looks like this (J
is shorthand for self join):
DT
x y v
1: a 1 1
2: a 3 2
3: a 6 3
4: b 1 4
5: b 3 5
6: b 6 6
7: c 1 7
8: c 3 8
9: c 6 9
DT[J("a", 4:6)] # v columns does not have "2".
x y v
1: a 4 NA
2: a 5 NA
3: a 6 3
DT[J("a", 4:6), roll = TRUE] # v column rolls "2" forward.
x y v
1: a 4 2
2: a 5 2
3: a 6 3
In Pandas, the closest that I could think of is to use Join then DataFrame,fillna(method='pad') . But this will not yield the above R rolling join result since the first 2 rows would be NA in the joined column (it starts with (a,3) and not (a,2))
I am just wondering whether there is an equivalent operation in Pandas for this?
Kind regards,
Kris