fix_missing_hours.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import copy
  2. import csv
  3. # instant,dteday,season,yr,mnth,hr,holiday,weekday,workingday,weathersit,temp,atemp,hum,windspeed,casual,registered,cnt
  4. with open('hour.csv', newline='') as hour_file, open('hour-fixed.csv', 'w', newline='') as fixed_file:
  5. hour_csv = csv.reader(hour_file)
  6. fixed_csv = csv.writer(fixed_file)
  7. last_row = None
  8. for this_row in hour_csv:
  9. if last_row is None:
  10. pass
  11. elif last_row[0] == 'instant':
  12. pass
  13. else:
  14. last_hour = int(last_row[5])
  15. this_hour = int(this_row[5])
  16. if this_hour < last_hour:
  17. this_hour += 24
  18. missing_row = copy.deepcopy(last_row)
  19. missing_row[-1] = 0
  20. for missing_hour in range(last_hour+1, this_hour):
  21. if missing_hour == 24:
  22. missing_row = copy.deepcopy(this_row)
  23. missing_row[-1] = 0
  24. missing_row[5] = missing_hour % 24
  25. fixed_csv.writerow(missing_row)
  26. print(last_hour, this_hour, missing_row)
  27. fixed_csv.writerow(this_row)
  28. last_row = this_row