Reputation: 433
I have two columns of format:
A | B |
---|---|
31-12-2010:10.06 | 05-01-2011:15.12 |
And using Python, I want to obtain the relative time between them (A - B
).
However, I get the error:
TypeError: unsupported operand type(s) for -: 'str' and 'str'
I tried to do: int(A) - int(B)
but it didn't work either:
TypeError: cannot convert the series to <class 'int'>
Can anyone please tell me how to do it?
Upvotes: 0
Views: 684
Reputation: 11128
You are getting the error because you are trying to operate a negative sign(subtraction) on two objects of str
data type. You need to convert them first to datetime object then only you can do mathematical operations.
You can try this as well, you can run along with my example given:
pd.to_datetime(df['a'], format = '%d-%m-%Y:%H.%M')
If you are wondering about %d, %m etc, no need to be afraid, they can be understood easily by reading this
However to convert more columns into datetime you can use apply like below:
df = df.apply(lambda x: pd.to_datetime(x, format='%d-%m-%Y:%H.%M'))
Once you have these columns as datetime, you can subtract them using:
df['a'] - df['b']
However if you want days then probably you can do this:
(df['b'] - df['a']).dt.days
Input data:
import pandas as pd
df = pd.DataFrame({"a": ["31-12-2010:10.06"], "b": ["05-01-2011:15.12"]})
Upvotes: 1