I have a table of this format :
date
0 2017-03-30 09:59:00
1 2017-03-30 09:59:30
2 2017-03-30 10:00:00
3 2017-03-30 10:00:30
4 2017-03-30 10:01:00
5 2017-03-30 10:01:30
6 2017-03-30 10:02:00
What I'd like to do is to count the days in my table (starting by 1) but rather than starting at midnight, I'd like my days to start at 10:00:00. So for the table above, the result would be :
date day
0 2017-03-30 09:59:00 0
1 2017-03-30 09:59:30 0
2 2017-03-30 10:00:00 1
3 2017-03-30 10:00:30 1
4 2017-03-30 10:01:00 1
5 2017-03-30 10:01:30 1
6 2017-03-30 10:02:00 1
So far, I managed to produce the new column with the following code :
d=1
hour_before = 10
col_days = []
for row in df.itertuples():
if row.date.hour == 10 and hour_before != 10 :
d+= 1
hour_before = row.date.hour
cols_days.append(d)
df['day'] = col_days
But I'd love to have a more elegant (and more importantly, quicker) way to do this operation.
Any suggestions ?