Reputation: 105
I am trying to take the time difference between two dates using pandas and am having some trouble.
The format the dates are in are as follows:
2016/07/06 03:10:39
Using pandas.to_datetime I can get these to a datetime64 type but then cannot figure out a way to take the difference of these types.
here is the basis of my code:
df2.End = to_datetime(df1.End)
df2.Start = to_datetime(df1.Start)
If I print these objects from the Dataframe here is what I get:
Series([], Name: End, dtype: datetime64[ns])
Series([], Name: Start, dtype: datetime64[ns])
End:
0 2016-07-06 04:39:16
1 2016-07-06 04:13:30
2 2016-07-06 03:51:30
Start
0 2016-07-06 05:01:14
1 2016-07-06 04:39:06
2 2016-07-06 04:13:27
I have tried to do the following:
df2.difference = df2.End-df2.Start
and get the following error:
TypeError: ufunc subtract cannot use operands with types dtype('<M8[ns]') and dtype('O')
Upvotes: 1
Views: 1028
Reputation: 8464
This works perfectly:
>>> import pandas
>>> x = pandas.to_datetime("2016/07/06 03:10:39")
>>> y = pandas.to_datetime("2016/07/06 03:11:21")
>>> y-x
Timedelta('0 days 00:00:42')
Post your code if you still can't figure what your problem.
Upvotes: 1