I have two columns, fromdate
and todate
, in a dataframe.
JavaScript
x
7
1
import pandas as pd
2
3
data = {'todate': [pd.Timestamp('2014-01-24 13:03:12.050000'), pd.Timestamp('2014-01-27 11:57:18.240000'), pd.Timestamp('2014-01-23 10:07:47.660000')],
4
'fromdate': [pd.Timestamp('2014-01-26 23:41:21.870000'), pd.Timestamp('2014-01-27 15:38:22.540000'), pd.Timestamp('2014-01-23 18:50:41.420000')]}
5
6
df = pd.DataFrame(data)
7
I add a new column, diff
, to find the difference between the two dates using
JavaScript
1
2
1
df['diff'] = df['fromdate'] - df['todate']
2
I get the diff
column, but it contains days
, when there’s more than 24 hours.
JavaScript
1
5
1
todate fromdate diff
2
0 2014-01-24 13:03:12.050 2014-01-26 23:41:21.870 2 days 10:38:09.820000
3
1 2014-01-27 11:57:18.240 2014-01-27 15:38:22.540 0 days 03:41:04.300000
4
2 2014-01-23 10:07:47.660 2014-01-23 18:50:41.420 0 days 08:42:53.760000
5
How do I convert my results to only hours and minutes (i.e. days are converted to hours)?
Advertisement
Answer
Pandas timestamp differences returns a datetime.timedelta object. This can easily be converted into hours by using the *as_type* method, like so
JavaScript
1
6
1
import pandas
2
df = pandas.DataFrame(columns=['to','fr','ans'])
3
df.to = [pandas.Timestamp('2014-01-24 13:03:12.050000'), pandas.Timestamp('2014-01-27 11:57:18.240000'), pandas.Timestamp('2014-01-23 10:07:47.660000')]
4
df.fr = [pandas.Timestamp('2014-01-26 23:41:21.870000'), pandas.Timestamp('2014-01-27 15:38:22.540000'), pandas.Timestamp('2014-01-23 18:50:41.420000')]
5
(df.fr-df.to).astype('timedelta64[h]')
6
to yield,
JavaScript
1
5
1
0 58
2
1 3
3
2 8
4
dtype: float64
5