Calculate Pandas DataFrame Time Difference Between Two Columns in Hours and Minutes

后端 未结 3 1568
甜味超标
甜味超标 2020-11-22 12:14

I have two columns, fromdate and todate, in a dataframe.

import pandas as pd

data = {\'todat         


        
3条回答
  •  孤城傲影
    2020-11-22 12:41

    This was driving me bonkers as the .astype() solution above didn't work for me. But I found another way. Haven't timed it or anything, but might work for others out there:

    t1 = pd.to_datetime('1/1/2015 01:00')
    t2 = pd.to_datetime('1/1/2015 03:30')
    
    print pd.Timedelta(t2 - t1).seconds / 3600.0
    

    ...if you want hours. Or:

    print pd.Timedelta(t2 - t1).seconds / 60.0
    

    ...if you want minutes.

提交回复
热议问题