0

I'm using python with pandas and datetime.

I have dataframe in csv file like that:

enter image description here

I too add new column called "Duration" by subtracting "Start date" from "End date".

what I have tried to do:

rides['Duration'] = end_time - start_time
duration = rides['Duration']

print(rides['Duration'])

and what I get is:

0     0 days 00:03:01
1     0 days 02:07:02
2     0 days 00:05:43

what I want to get is:

0     00:03:01
1     02:07:02
2     00:05:43

That is, I want to remove the "days" and show only the time.

  • Does this answer your question? [Remove the days in the timedelta object](https://stackoverflow.com/questions/53129971/remove-the-days-in-the-timedelta-object). – Henry Ecker Jun 05 '21 at 17:40

2 Answers2

0

You can use % format specifier to extract hour, minute and second.

print("%d:%d:%d"%(rides['Duration'].hour,rides['Duration'].minute,rides['Duration'].second))
Suchitra
  • 101
  • 1
  • 3
0

Try via rsplit() if you want 'Duration' column of type object:

rides['Duration']=rides['Duration'].astype(str).str.rsplit(' ',1).str[1].str.strip('+|-')

OR

Try via rsplit() and to_datetime() if you want 'Duration' column of type datetime.time:

rides['Duration']=pd.to_datetime(rides['Duration'].astype(str).str.rsplit(' ',1).str[1].str.strip('+|-')).dt.time
Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41