I have data for 2018 and 2017 that uses 24:00 hour times like this:
Hour Ending X Y Z
12/31/2017 24:00 11452.16 1834.87 2856.94
I don't know how your data is structured, but just iterate through it and splice the string (if it's a string) to see if '24' shows up in the 5th to last chars to the 3rd to last char. Then add one to the date using the datetime module.
import datetime
lines = ['12/31/2017 24:00', '12/31/2017 23:00', '12/30/2017 24:00']
for line in lines:
print('PREVIOUS : ' + line)
# Convert a 24 hour to 0 hour
if line[-5:-3] == '24':
(date, time) = line.split()
time = time[:-5] + '0' + time[-3:]
date = datetime.datetime.strptime(date, '%m/%d/%Y') + datetime.timedelta(days=1)
print('CHANGED : ' + date.strftime('%m/%d/%Y') + ' ' + time)
else:
print('UNCHANGED: ' + line)
output:
PREVIOUS : 12/31/2017 24:00
AFTER : 01/01/2018 0:00
PREVIOUS : 12/31/2017 23:00
UNCHANGED: 12/31/2017 23:00
PREVIOUS : 12/30/2017 24:00
AFTER : 12/31/2017 0:00
You can use pd.to_timedelta
to help:
s = df['Hour Ending'].str.split()
df['Hour Ending'] = pd.to_datetime(s.str[0]) + pd.to_timedelta(s.str[1].str.split(':').str[0] +' hours')
print(df)
Output:
Hour Ending X Y Z
0 2018-01-01 00:00:00 11452.16 1834.87 2856.94
1 2017-12-31 23:00:00 11579.85 1855.94 2898.57
2 2017-12-31 22:00:00 11754.25 1890.36 2942.23
3 2017-12-31 21:00:00 11883.11 1907.59 2970.85
4 2017-12-31 20:00:00 12015.66 1910.72 2989.82
5 2017-12-31 19:00:00 12061.55 1923.56 3002.77
6 2017-12-31 18:00:00 11663.43 1891.09 2915.28
7 2017-12-31 17:00:00 11008.23 1803.92 2871.94
8 2017-12-31 16:00:00 10904.93 1730.49 2864.33
9 2017-12-31 15:00:00 11014.92 1673.37 2862.77
10 2017-12-31 14:00:00 11099.28 1604.28 2853.98
11 2017-12-31 13:00:00 11088.55 1585.55 2841.05
12 2017-12-31 12:00:00 10989.86 1578.52 2822.75
13 2017-12-31 11:00:00 10849.49 1578.38 2802.30
14 2017-12-31 10:00:00 10600.86 1581.44 2774.18
15 2017-12-31 09:00:00 10184.76 1532.89 2715.56
16 2017-12-31 08:00:00 9826.52 1461.63 2672.01
17 2017-12-31 07:00:00 9556.41 1399.10 2611.86
18 2017-12-31 06:00:00 9260.16 1341.11 2578.80
19 2017-12-31 05:00:00 9113.75 1328.50 2581.56
20 2017-12-31 04:00:00 9025.76 1346.87 2582.43
21 2017-12-31 03:00:00 9044.65 1343.63 2584.13
22 2017-12-31 02:00:00 9194.51 1358.57 2600.79
23 2017-12-31 01:00:00 9444.48 1379.35 2621.97
24 2017-12-31 00:00:00 9794.90 1426.91 2679.92