1

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?

Boosted_d16
  • 13,340
  • 35
  • 98
  • 158
  • 2
    You'd want to first convert the date columns via `pd.to_datetime()` and then take the diff? – Zero Oct 21 '15 at 14:08

1 Answers1

3

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.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677