Reputation: 9909
Given the following df
in which timestamps are not index:
timestamp
0 2020-10-23 12:20:00-04:00
1 2020-10-23 12:30:00-04:00
2 2020-10-23 12:40:00-04:00
3 2020-10-23 12:50:00-04:00
I am trying to create a new column minutes
that contains the total number of minutes as counted from 00:00:00.
So the above would output
timestamp minutes
0 2020-10-23 12:20:00-04:00 740
1 2020-10-23 12:30:00-04:00 750
2 2020-10-23 12:40:00-04:00 760
3 2020-10-23 12:50:00-04:00 770
I've been trying pd.timedelta however I'm unable to set a begin time to reference from (in my case I'd want the minutes counted from 00:00:00 hs
). Importantly, minutes
should be integer and not string.
Any suggestions on how to tackle this?
Upvotes: 1
Views: 37
Reputation: 150735
You can use dt.normalize
to get the day, subtract, and divide the timdelta 1T
:
df['minutes'] = (df.timestamp - df.timestamp.dt.normalize()) // pd.Timedelta('1T')
Another option (obviously easy):
df['minutes'] = df.timestamp.dt.hour * 60 + df.timestamp.dt.minute
Output:
timestamp minutes
0 2020-10-23 12:20:00-04:00 740
1 2020-10-23 12:30:00-04:00 750
2 2020-10-23 12:40:00-04:00 760
3 2020-10-23 12:50:00-04:00 770
Upvotes: 1