Skip to content
Advertisement

Calculate Time Difference Between Two Pandas Columns in Hours and Minutes

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

JavaScript

I add a new column, diff, to find the difference between the two dates using

JavaScript

I get the diff column, but it contains days, when there’s more than 24 hours.

JavaScript

How do I convert my results to only hours and minutes (i.e. days are converted to hours)?

Advertisement

Answer

Pandas timestamp differences returns a datetime.timedelta object. This can easily be converted into hours by using the *as_type* method, like so

JavaScript

to yield,

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement