I have data from Pandas which was the contents of a CSV file:
JavaScript
x
9
1
Date Inbound Outbound
2
17/10/2019 12:35 5.49E+02 3.95E+03
3
17/10/2019 12:40 2.06E+02 3.17E+03
4
17/10/2019 12:45 2.06E+02 3.17E+03
5
17/10/2019 12:50 2.06E+02 3.17E+03
6
17/10/2019 12:55 2.06E+02 3.17E+03
7
17/10/2019 13:00 2.06E+02 3.17E+03
8
.
9
I aim to convert the column Date
from timestamps to time periods in units of minutes, which should result in something like the following:
JavaScript
1
8
1
Date Inbound Outbound
2
0 5.49E+02 3.95E+03
3
5 2.06E+02 3.17E+03
4
10 2.06E+02 3.17E+03
5
15 2.06E+02 3.17E+03
6
20 2.06E+02 3.17E+03
7
25 2.06E+02 3.17E+03
8
Advertisement
Answer
You can use subtract the first timestampe to calculate the difference, then get total_seconds()
and convert to minutes:
JavaScript
1
6
1
df['Date'] = pd.to_datetime(df['Date'])
2
3
df['Date'] = (df.Date.sub(df.Date.iloc[0])
4
.dt.total_seconds().div(60).astype(int)
5
)
6
Output:
JavaScript
1
8
1
Date Inbound Outbound
2
0 0 549.0 3950.0
3
1 5 206.0 3170.0
4
2 10 206.0 3170.0
5
3 15 206.0 3170.0
6
4 20 206.0 3170.0
7
5 25 206.0 3170.0
8