I want to apply groupby with a time window of 60 minutes but it only collects the value in the hour it has appeared and does not show anything for a window where there is no value.
I want it in a way that for the window without any value it gives 0 so as to have the data in a more continuous fashion.
for example:
df = sc.parallelize(
[Row(datetime='2015/01/01 03:00:36', value = 2.0),
Row(datetime='2015/01/01 03:40:12', value = 3.0),
Row(datetime='2015/01/01 05:25:30', value = 1.0)]).toDF()
df1 = df.select(sf.unix_timestamp(sf.column("datetime"), 'yyyy/MM/dd HH:mm:ss').cast(TimestampType()).alias("timestamp"), sf.column("value"))
df1.groupBy(sf.window(sf.col("timestamp"), "60 minutes")).agg(sf.sum("value")).show(truncate = False)
the output i get is:
+------------------------------------------+----------+
|window |sum(value)|
+------------------------------------------+----------+
|[2015-01-01 03:00:00, 2015-01-01 04:00:00]|5.0 |
|[2015-01-01 05:00:00, 2015-01-01 06:00:00]|1.0 |
+------------------------------------------+----------+
whereas, I would rather want the output to be:
+------------------------------------------+----------+
|window |sum(value)|
+------------------------------------------+----------+
|[2015-01-01 03:00:00, 2015-01-01 04:00:00]|5.0 |
|[2015-01-01 04:00:00, 2015-01-01 05:00:00]|0.0 |
|[2015-01-01 05:00:00, 2015-01-01 06:00:00]|1.0 |
+------------------------------------------+----------+
Edit:
How do i then extend it to double groupby and equal number of windows for each "name":
df = sc.parallelize(
[Row(name = 'ABC', datetime = '2015/01/01 03:00:36', value = 2.0),
Row(name = 'ABC', datetime = '2015/01/01 03:40:12', value = 3.0),
Row(name = 'ABC', datetime = '2015/01/01 05:25:30', value = 1.0),
Row(name = 'XYZ', datetime = '2015/01/01 05:15:30', value = 2.0)]).toDF()
df1 = df.select('name', sf.unix_timestamp(sf.column("datetime"), 'yyyy/MM/dd HH:mm:ss').cast(TimestampType()).alias("timestamp"), sf.column("value"))
df1.show(truncate = False)
>>>+----+-------------------+-----+
|name|timestamp |value|
+----+-------------------+-----+
|ABC |2015-01-01 03:00:36|2.0 |
|ABC |2015-01-01 03:40:12|3.0 |
|ABC |2015-01-01 05:25:30|1.0 |
|XYZ |2015-01-01 05:15:30|2.0 |
+----+-------------------+-----+
and i want the result to be:
+----+------------------------------------------+----------+
|name|window |sum(value)|
+----+------------------------------------------+----------+
|ABC |[2015-01-01 03:00:00, 2015-01-01 04:00:00]|5.0 |
|ABC |[2015-01-01 04:00:00, 2015-01-01 05:00:00]|0.0 |
|ABC |[2015-01-01 05:00:00, 2015-01-01 06:00:00]|1.0 |
|XYZ |[2015-01-01 03:00:00, 2015-01-01 04:00:00]|0.0 |
|XYZ |[2015-01-01 04:00:00, 2015-01-01 05:00:00]|0.0 |
|XYZ |[2015-01-01 05:00:00, 2015-01-01 06:00:00]|2.0 |
+----+------------------------------------------+----------+