1

I am trying to create a new column based on a timestamp column in Python using pandas and datetime libraries.

I have tried using datetime, as other methods I have tried have not given the results closest to what I want. It seems so simple but I cant figure it out.

This is the closest I have gotten to what I want so far:

import datetime as dt

datetime.datetime.now().minute

datetime.datetime.now().hour

minute1 = dfclean['Time Call Was Received'].dt.minute

minute2 = dfclean['Time Vehicle was Dispatched'].dt.minute

minute3 = dfclean['Time Vehicle was en Route to Scene'].dt.minute

minute4 = dfclean['Time Arrived on Scene'].dt.minute

minute5 = dfclean['Time Arrived at Patient'].dt.minute

minute6 = dfclean['Time Departed from the Scene'].dt.minute

minute7 = dfclean['Time Arrived at Hospital'].dt.minute

dfclean['Mins Call Received to Vehicle Dispatched'] = minute2 - minute1

dfclean['Mins Vehicle En Route to Arrival On Scene'] = minute4 - minute3

dfclean['Mins Vehicle Arrived at Patient to Departing From Scene'] = minute6 - minute5

dfclean['Mins Departing Scene to Arriving at Hospital'] = minute7 - minute6

Here is the output I am talking about

However, this returns negative values since it is only extracting the minutes. So if the timestamp is subtracting 9:00 am from 8:51am I get -49. Is there a way to include hours in the subtraction so it doesnt account for the negative number? Or maybe a completely different approach that is easier?

Any help is greatly appreciated!

I want the output to be exact minutes, single number.

Analyst101
  • 11
  • 2

1 Answers1

0

Maybe you could use timedelta:

minute1 = timedelta(minutes=dfclean['Time Call Was Received'].dt.minute)

For reference:

Understanding timedelta

Eric Grinstein
  • 291
  • 2
  • 7
  • So I tried this code, and got "module is not callable", so I imported timedelta from datetime, and changed it to "datetime.timedelta(minutes=dfclean..." but am now getting the same error I have before which is from "dfclean['Time Call was..." type being a series in the dataframe. Is there any way around this error? Thanks for helping! – Analyst101 Jun 07 '19 at 20:15
  • Sorry, I wasn't as clear as I should have. You could put in the hours as well in the timedelta: e.g.: `timedelta(hours=dfclean['....'].dt.hour, minutes=dfclean['....'].dt.minute )`. Maybe it will work now. – Eric Grinstein Jun 07 '19 at 20:28