简体   繁体   中英

how to pass a pandas dataframe column from datetime to time (HH:MM:SS) in python

I have a df that looks like this:

           date          arrival_time       departure_time
0     2021-01-16  2021-01-16 16:58:16  2021-01-16 21:02:19
1     2021-01-16  2021-01-16 22:34:04  2021-01-16 22:34:51
2     2021-01-27  2021-01-27 16:01:21  2021-01-27 16:02:22
3     2021-01-07  2021-01-07 11:57:08  2021-01-07 14:22:15
4     2021-01-07  2021-01-07 12:20:54  2021-01-07 13:41:43

date , arrival_time and departure_time columns are datetime64[ns] columns. What I want to do is to convert arrival_time and departure_time columns to time with this format: HH:MM:SS . Therefore, my desired output should look something like this:

         date     arrival_time   departure_time 
0     2021-01-16    16:58:16      21:02:19
1     2021-01-16    22:34:04      22:34:51
2     2021-01-27    16:01:21      16:02:22
3     2021-01-07    11:57:08      14:22:15
4     2021-01-07    12:20:54      13:41:43

I've tried this code:

df["arrival_time"] = pd.to_datetime(df['arrival_time']).dt.strftime("%H:%M:%S")
df["departure_time"] = pd.to_datetime(df['departure_time']).dt.strftime("%H:%M:%S")

And I've also tried this one:

df['arrival_time'] = pd.to_datetime(df['arrival_time'], format="%Y-%m-%d %H:%M:%S").dt.strftime("%H:%M:%S")

However both approaches return a string, not a time object. If someone knows how to do that I would really appreciate your help!!!

To get the values as 'datetime.time' object you can use .time like so:

df["arrival_time"] = pd.to_datetime(df['arrival_time'], format="%Y-%m-%d %H:%M:%S").dt.time
df["departure_time"] = pd.to_datetime(df['departure_time'], format="%Y-%m-%d %H:%M:%S").dt.time

print(df)
print(df['departure_time'][0].__class__) # print element class

Output:

         date arrival_time departure_time
0  2021-01-16     16:58:16       21:02:19
1  2021-01-16     22:34:04       22:34:51
2  2021-01-27     16:01:21       16:02:22
3  2021-01-07     11:57:08       14:22:15
4  2021-01-07     12:20:54       13:41:43
<class 'datetime.time'>

It is possible to split() the string by column and then apply the datetime conversion over pandas dataframe specific columns:

import pandas as pd
import re
import datetime as dt

# splitting strings #
df['arrival_time'] = df['arrival_time'].str.split(' ', expand=True)[1]
df['departure_time'] = df['departure_time'].str.split(' ', expand=True)[1]

# converting to specific datetime format #

df["arrival_time"] = pd.to_datetime(df['arrival_time'],format="%H:%M:%S")
df["departure_time"] = pd.to_datetime(df['departure_time'], format="%H:%M:%S")

Console output:

#   date      arrival_time  departure_time
#0  2021-01-16  16:58:16    21:02:19

data

d = {'date': ['2021-01-16'], 'arrival_time': ['2021-01-16 16:58:16'], 'departure_time':['2021-01-16 21:02:19']}
df = pd.DataFrame(data=d)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM