说我在DataFrame中有一列排序的时间戳。我想编写一个向此DataFrame添加一列的函数,该函数根据以下规则将时间戳切成连续的时间片:
英语:每个组的行数不得超过n行,跨度不应超过t时间
例如:(使用整数简化时间戳记)
输入
time
---------
1
2
3
5
10
100
2000
2001
2002
2003
输出(在具有n = 3和t = 5的限幅函数之后)
time | group
----------|------
1 | 1
2 | 1
3 | 1
5 | 2 // cut because there were no cuts in the last 3 rows
10 | 2
100 | 3 // cut because 100 - 5 > 5
2000 | 4 // cut because 2000 - 100 > 5
2001 | 4
2002 | 4
2003 | 5 // cut because there were no cuts in the last 3 rows
我感觉这可以通过Spark中的窗口函数来完成。毕竟,创建了窗口函数以帮助开发人员计算移动平均值。您基本上可以计算出n行的每个窗口的一列(股价)的合计(在这种情况下为平均值)。
同样应该能够在这里完成。对于每行,如果最后n行不包含剪切,或者最后剪切与当前时间戳之间的时间间隔大于t,则cut = true, o.w. cut = false
。但是我似乎无法弄清楚的是如何使Window Function意识到自身。就像特定行的移动平均知道最后一个移动平均。