How can I convert a datatime into a time decimal

Question:

I have two columns, Picking End Time and Picking Start Time.

First I converted them to datetime

data['Picking Start Time'] = pd.to_datetime(data['Picking Start Time'])
data['Picking End Time'] = pd.to_datetime(data['Picking End Time'])

After that I wanted to see the picking time

data['Picking Time'] = data['Picking End Time'] - data['Picking Start Time']

To strip the date and have just the time I used

data['Picking Time'] = data['Picking Time'].astype(str).map(lambda x: x[7:])

Now, my next challenge is to transform the Picking Time into Decimal Time, but I cannot find anything that can help.

Asked By: Dan-Alexandru Jecu

||

Answers:

First remove:

data['Picking Time'] = data['Picking Time'].astype(str).map(lambda x: x[7:])

Use Series.dt.total_seconds and if necessary divide by scalar 60 or 3600:

#seconds
data['Picking Time'] = data['Picking Time'].dt.total_seconds()

#minutes
data['Picking Time'] = data['Picking Time'].dt.total_seconds() / 60

#hours
data['Picking Time'] = data['Picking Time'].dt.total_seconds() / 3600
Answered By: jezrael
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.