This question evaluates proficiency in pandas-based data manipulation—specifically windowed aggregations with strict boundary conditions and group-wise temporal interval merging—demonstrating skills in rolling/window operations, grouping, sorting, and handling time-based overlaps.
You are given two independent pandas tasks.
df
row_id
INT — unique row order key, already sorted ascending
value
FLOAT
k >= 0
, compute for each row the average of the values from the previous
k
rows, the current row, and the next
k
rows.
k
previous rows and
k
next rows, set the output to
-1
for that row.
row_id
,
value
,
window_avg
.
segments
vehicle_id
STRING
event_type
STRING
start_ts
TIMESTAMP
end_ts
TIMESTAMP
start_ts <= end_ts
for every row.
(vehicle_id, event_type)
independently, merge intervals that overlap or touch, where a new interval should be merged into the current one if
next.start_ts <= current.end_ts
.
vehicle_id
,
event_type
,
merged_start_ts
,
merged_end_ts
, sorted by
vehicle_id
,
event_type
,
merged_start_ts
.
Write pandas code for both tasks.