pandas: calculate time difference between df columns [duplicate]

孤街浪徒 提交于 2021-02-11 02:23:22

问题


I have two df columns with string values:

df['starttime']                           df['endtime']

0            2015-10-06 18:35:33            0            2015-10-06 18:35:58
1     2015-10-08 17:51:21.999000            1            2015-10-08 17:52:10
2     2015-10-08 20:51:55.999000            2            2015-10-08 20:52:21
3     2015-10-05 15:16:49.999000            3            2015-10-05 15:17:00
4     2015-10-05 15:16:53.999000            4            2015-10-05 15:17:22
5     2015-10-05 15:17:11.999000            5     2015-10-05 15:17:23.999000

Id like to calculate the difference between these two columns

here is what I tried but failed:

(df['starttime']-df['endtime']).astype('timedelta64[h]'))

unsupported operand type(s) for -: 'str' and 'str'

I thought astype would convert the str to timedelta?


回答1:


Convert the date strings to pandas.Timestamps:

df['starttime'] = pd.to_datetime(df['starttime'])
df['endtime'] = pd.to_datetime(df['endtime'])

Then take the difference:

df['starttime']-df['endtime']

unsupported operand type(s) for -: 'str' and 'str'

occurs when you try to subtract two Series containing strings:

df['starttime']-df['endtime']

without first converting the strings to Timestamps.



来源:https://stackoverflow.com/questions/33261397/pandas-calculate-time-difference-between-df-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!