Reputation: 245
So, I would like create a new column called df["Diff"]
by getting the difference between two datetime columns. My code is, thus:
df["Diff"] = df["Time"] - df_target["Time"]
When I run this code, I get the following error-message:
TypeError: Cannot compare type 'Timestamp' with type 'int'
I have however made sure that both columns are datetime-data type using .dtype
.
I have also checked for any integer-values in any of the columns, but cannot find any.
Here is an example of the data for the two columns:
df.Time
Time
2017-09-01 01:31:10.000 2017-09-01 01:31:10
df_target
Time
2017-12-01 22:17:00 2017-12-01 22:17:00
Upvotes: 2
Views: 3588
Reputation: 245
Okay, so I figured out the answer.
Since one of the dataframes had an index based on datetime, while the other had an index on integer, I used reset_index to make both dataframes have an index consisting of integers. This then let me use the above code mentioned.
df["Diff"] = df["Time"] - df_target["Time"]
Thank you for all your help!
Upvotes: 2
Reputation: 13
Pandas timestamp differences returns a datetime.timedelta object. This can easily be converted into hours by using the as_type method, like so
import pandas
df = pandas.DataFrame(columns=['to','fr','ans'])
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')]
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')]
(df.fr-df.to).astype('timedelta64[h]')
to yield,
0 58
1 3
2 8
dtype: float64
Upvotes: 0
Reputation: 720
Convert both columns with dates to pd.to_datetime
and then perform the operation.
Upvotes: 0