pandas difference of time columns

Solutions on MaxInterview for pandas difference of time columns by the best coders in the world

showing results for - "pandas difference of time columns"
Deandre
09 Jun 2019
1df = pd.DataFrame(columns=["one", "two"])
2
3df.one = ["2019-01-24","2019-01-27"]
4df.two = ["2019-01-28", "2020-01-29"]
5
6df.one = pd.to_datetime(df.one)
7df.two = pd.to_datetime(df.two)
8
9print(df)
10# OUTPUT
11#          one        two
12# 0 2019-01-24 2019-01-28
13# 1 2019-01-27 2020-01-29
14
15difference = (df.two - df.one)
16
17print(difference)
18# OUTPUT
19# 0     4 days
20# 1   367 days
21# dtype: timedelta64[ns]