Reputation: 1258
I've got the following data frame on pandas:
d = {'col_Date_Time': ['2020-08-01 00:00:00',
'2020-08-01 00:10:00',
'2020-08-01 00:15:00',
'2020-08-01 00:19:00',
'2020-08-01 01:19:00',
'2020-08-02 00:00:00',
'2020-08-02 00:15:00',
'2020-08-02 00:35:00',
'2020-08-02 01:35:00']}
df = pd.DataFrame(data=d)
df = pd.to_datetime(df.col_Date_Time)
I want to add another column that contains the number of minutes from the start of each day.
So, the result in this case woud be:
NAN 10 15 19 79 NAN 15 35 95
Upvotes: 0
Views: 518
Reputation: 521
import pandas as pd
import numpy as np
df = pd.DataFrame({'col_Date_Time': ['2020-08-01 00:00:00',
'2020-08-01 00:10:00',
'2020-08-01 00:15:00',
'2020-08-01 00:19:00',
'2020-08-01 01:23:00',
'2020-08-02 00:00:00',
'2020-08-02 00:15:00',
'2020-08-02 00:35:00',
'2020-08-02 06:31:00']})
df['col_Date_Time'] = pd.to_datetime(df.col_Date_Time)
df['start_day_time_stamp']=list(map(lambda x: x.date(),df['col_Date_Time']))
df['mins_from_day_start']=((pd.to_datetime(df['col_Date_Time'])-pd.to_datetime(df['start_day_time_stamp'])).dt.total_seconds())/60
df
Upvotes: 1
Reputation: 651
You can truncate the column to days (.dt.floor('d')), subtract this to col_Date_Time, and save in another column:
df["DELTA"] = df.col_Date_Time - df.col_Date_Time.dt.floor('d')
If you want this like integer:
df["DELTA2"] = df.DELTA.dt.seconds.div(60).astype(int)
col_Date_Time DELTA DELTA2
0 2020-08-01 00:00:00 00:00:00 0
1 2020-08-01 00:10:00 00:10:00 10
2 2020-08-01 00:15:00 00:15:00 15
3 2020-08-01 00:19:00 00:19:00 19
4 2020-08-01 01:19:00 01:19:00 79
5 2020-08-02 00:00:00 00:00:00 0
6 2020-08-02 00:15:00 00:15:00 15
7 2020-08-02 00:35:00 00:35:00 35
8 2020-08-02 01:35:00 01:35:00 95
Upvotes: 1
Reputation: 323326
Let us try
s = df.dt.minute.where(df.dt.date.duplicated())
Out[66]:
0 NaN
1 10.0
2 15.0
3 19.0
4 NaN
5 15.0
6 35.0
Name: col_Date_Time, dtype: float64
Upvotes: 1