I am trying to compute which rows overlaps in time with any other row on a DataFrame. Given this example:
df = spark.createDataFrame(
[
[1,"2020:01:01 12:00", "2020:01:01 13:00"], # T
[1,"2020:01:01 12:30", "2020:01:01 13:00"], # T
[1,"2020:01:01 14:00", "2020:01:01 15:00"], # F
[2,"2020:01:01 09:00", "2020:01:01 13:00"], # F
[2,"2020:01:01 18:00", "2020:01:01 19:00"], # F
],
["id", "start", "end"]
)
w = Window.partitionBy("id").orderBy("start").rowsBetween(Window.unboundedPreceding, 0)
df.withColumn("OVERLAPS", ?????.over(w)).show()
I would like to generate a column that signal whenever two rows with the same id overlaps in time:
[1,"2020:01:01 12:00", "2020:01:01 13:00", True],
[1,"2020:01:01 12:30", "2020:01:01 13:00", True],
[1,"2020:01:01 14:00", "2020:01:01 15:00", False],
[2,"2020:01:01 09:00", "2020:01:01 13:00", False],
[2,"2020:01:01 18:00", "2020:01:01 19:00", False],
Ideally I could create time intervals, in pandas it would be using pd.IntervalIndex and I would have intervals which I could query, for example "2020:01:01 13:00" in [2020:01:01 12:30, 2020:01:01 14:00)
But I've been struggling to do it on spark.