Fill a gap with that device own average

Data framesMedium11 min

Your task

Given readings with columns device, ts and reading, replace each missing reading with the mean of that device other readings. A device with no readings at all keeps null. Return device, ts and reading, sorted by device ascending and then ts ascending.

Examples

fill_with_device_mean(readings: dataframe) -> dataframe
Example 1
Inputfill_with_device_mean(readings)
readings
devicetsreading
a12024-05-0120.5
a12024-05-02NULL
a12024-05-0322.5
b22024-05-01NULL
b22024-05-0231
b22024-05-0333
Output
devicetsreading
a12024-05-0120.5
a12024-05-0221.5
a12024-05-0322.5
b22024-05-0132
b22024-05-0231
b22024-05-0333