Home > Back-end >  Is there a method that replace NaN values with values of previous 24 hours?
Is there a method that replace NaN values with values of previous 24 hours?

Time:02-10

I would like to replace the value NaN of day 20211021 - HOUR 1 with the value of day 20211020 - HOUR 1, the value of day day 20211021 - HOUR 2 with the value of day 20211020 - HOUR 2...

timestamp data year month day hour solar_total
2021-10-20 00:00:00 20211020 2021 10 20 1 0.0
2021-10-20 01:00:00 20211020 2021 10 20 2 0.0
2021-10-20 02:00:00 20211020 2021 10 20 3 0.0
2021-10-20 03:00:00 20211020 2021 10 20 4 0.0
2021-10-20 04:00:00 20211020 2021 10 20 5 0.0
2021-10-20 05:00:00 20211020 2021 10 20 6 0.0
2021-10-20 06:00:00 20211020 2021 10 20 7 0.0
2021-10-20 07:00:00 20211020 2021 10 20 8 65.0
2021-10-20 08:00:00 20211020 2021 10 20 9 1498.0
2021-10-20 09:00:00 20211020 2021 10 20 10 4034.0
2021-10-20 10:00:00 20211020 2021 10 20 11 6120.0
2021-10-20 11:00:00 20211020 2021 10 20 12 7450.0
2021-10-20 12:00:00 20211020 2021 10 20 13 7943.0
2021-10-20 13:00:00 20211020 2021 10 20 14 7821.0
2021-10-20 14:00:00 20211020 2021 10 20 15 7058.0
2021-10-20 16:00:00 20211020 2021 10 20 17 3664.0
2021-10-20 17:00:00 20211020 2021 10 20 18 1375.0
2021-10-20 18:00:00 20211020 2021 10 20 19 11.0
2021-10-20 19:00:00 20211020 2021 10 20 20 0.0
2021-10-20 20:00:00 20211020 2021 10 20 21 0.0
2021-10-20 21:00:00 20211020 2021 10 20 22 0.0
2021-10-20 22:00:00 20211020 2021 10 20 23 0.0
2021-10-20 23:00:00 20211020 2021 10 20 24 0.0
2021-10-21 00:00:00 20211021 2021 10 21 1 NaN
2021-10-21 01:00:00 20211021 2021 10 21 2 NaN
2021-10-21 02:00:00 20211021 2021 10 21 3 NaN
2021-10-21 03:00:00 20211021 2021 10 21 4 NaN
2021-10-21 04:00:00 20211021 2021 10 21 5 NaN
2021-10-21 05:00:00 20211021 2021 10 21 6 NaN
2021-10-21 06:00:00 20211021 2021 10 21 7 NaN
2021-10-21 07:00:00 20211021 2021 10 21 8 NaN
2021-10-21 08:00:00 20211021 2021 10 21 9 NaN
2021-10-21 09:00:00 20211021 2021 10 21 10 NaN
2021-10-21 10:00:00 20211021 2021 10 21 11 NaN
2021-10-21 11:00:00 20211021 2021 10 21 12 NaN
2021-10-21 12:00:00 20211021 2021 10 21 13 NaN
2021-10-21 13:00:00 20211021 2021 10 21 14 NaN
2021-10-21 14:00:00 20211021 2021 10 21 15 NaN
2021-10-21 15:00:00 20211021 2021 10 21 16 NaN
2021-10-21 16:00:00 20211021 2021 10 21 17 NaN
2021-10-21 17:00:00 20211021 2021 10 21 18 NaN
2021-10-21 18:00:00 20211021 2021 10 21 19 NaN
2021-10-21 19:00:00 20211021 2021 10 21 20 NaN
2021-10-21 20:00:00 20211021 2021 10 21 21 NaN
2021-10-21 21:00:00 20211021 2021 10 21 22 NaN
2021-10-21 22:00:00 20211021 2021 10 21 23 NaN
2021-10-21 23:00:00 20211021 2021 10 21 24 NaN

CodePudding user response:

If you have no missing timestamp, shift your rows by 23:

df['solar_total'] = df['solar_total'].fillna(df['solar_total'].shift(23))
timestamp data year month day hour solar_total
2021-10-20 00:00:00 20211020 2021 10 20 1 0
2021-10-20 01:00:00 20211020 2021 10 20 2 0
2021-10-20 02:00:00 20211020 2021 10 20 3 0
2021-10-20 03:00:00 20211020 2021 10 20 4 0
2021-10-20 04:00:00 20211020 2021 10 20 5 0
2021-10-20 05:00:00 20211020 2021 10 20 6 0
2021-10-20 06:00:00 20211020 2021 10 20 7 0
2021-10-20 07:00:00 20211020 2021 10 20 8 65
2021-10-20 08:00:00 20211020 2021 10 20 9 1498
2021-10-20 09:00:00 20211020 2021 10 20 10 4034
2021-10-20 10:00:00 20211020 2021 10 20 11 6120
2021-10-20 11:00:00 20211020 2021 10 20 12 7450
2021-10-20 12:00:00 20211020 2021 10 20 13 7943
2021-10-20 13:00:00 20211020 2021 10 20 14 7821
2021-10-20 14:00:00 20211020 2021 10 20 15 7058
2021-10-20 16:00:00 20211020 2021 10 20 17 3664
2021-10-20 17:00:00 20211020 2021 10 20 18 1375
2021-10-20 18:00:00 20211020 2021 10 20 19 11
2021-10-20 19:00:00 20211020 2021 10 20 20 0
2021-10-20 20:00:00 20211020 2021 10 20 21 0
2021-10-20 21:00:00 20211020 2021 10 20 22 0
2021-10-20 22:00:00 20211020 2021 10 20 23 0
2021-10-20 23:00:00 20211020 2021 10 20 24 0
2021-10-21 00:00:00 20211021 2021 10 21 1 0
2021-10-21 01:00:00 20211021 2021 10 21 2 0
2021-10-21 02:00:00 20211021 2021 10 21 3 0
2021-10-21 03:00:00 20211021 2021 10 21 4 0
2021-10-21 04:00:00 20211021 2021 10 21 5 0
2021-10-21 05:00:00 20211021 2021 10 21 6 0
2021-10-21 06:00:00 20211021 2021 10 21 7 0
2021-10-21 07:00:00 20211021 2021 10 21 8 65
2021-10-21 08:00:00 20211021 2021 10 21 9 1498
2021-10-21 09:00:00 20211021 2021 10 21 10 4034
2021-10-21 10:00:00 20211021 2021 10 21 11 6120
2021-10-21 11:00:00 20211021 2021 10 21 12 7450
2021-10-21 12:00:00 20211021 2021 10 21 13 7943
2021-10-21 13:00:00 20211021 2021 10 21 14 7821
2021-10-21 14:00:00 20211021 2021 10 21 15 7058
2021-10-21 15:00:00 20211021 2021 10 21 16 3664
2021-10-21 16:00:00 20211021 2021 10 21 17 1375
2021-10-21 17:00:00 20211021 2021 10 21 18 11
2021-10-21 18:00:00 20211021 2021 10 21 19 0
2021-10-21 19:00:00 20211021 2021 10 21 20 0
2021-10-21 20:00:00 20211021 2021 10 21 21 0
2021-10-21 21:00:00 20211021 2021 10 21 22 0
2021-10-21 22:00:00 20211021 2021 10 21 23 0
2021-10-21 23:00:00 20211021 2021 10 21 24 nan
  • Related