Kieran
Kieran

Reputation: 11

Convert string to HH:MM time in Python

I am new to Python so apologies if this is a simple fix.

I currently have a column of times that are currently stored as strings and look as below when I view my dataframe:

bus_no   time
Bus1     2.0
Bus2     840.0
Bus3     2340.0
Bus4     15.0
Bus5     1205.0
Bus6     1304.0
Bus7     1620.0
Bus8     9.0

So 9.0 equates to 00:09, 1620 to 16:20. (It is a rather large dataset with many more fields so I created that example to easily show the format it is showing)

Each time I have tried to convert it to time it also forms a date and merges part of the time into the date thus producing an inaccurate output. Any help would be appreciated.

Upvotes: 1

Views: 3253

Answers (5)

Selvaraj S.
Selvaraj S.

Reputation: 134

Thouse who want to convert '1:27 PM' to 24 hour format

from datetime import datetime
def twelve_to_twentyfour(t):
"""
input: t '1:27 PM'
output '13:27'
"""
in_time = datetime.strptime(t, "%I:%M %p")
out_time = datetime.strftime(in_time, "%H:%M")
return out_time

df['time'].apply(lambda x: twelve_to_twentyfour(x))

Upvotes: 0

leiberl
leiberl

Reputation: 83

It really depends on what you are after and the format of the data in the time column.

From the sample you give, it seems like your time column only includes float. But let's assume that it could as well include int and str formatted data.

Let's also assume that your dataframe is defined as following

>>>df.head()

    time
0      2
1 1620.0
2    155
3    120
4  123.0

Then you could first convert the time column to string with the following command

df.time = df.time.astype(str).astype(float).astype(int).astype(str)

Which now includes the time as string formatted int. Then you can add leading zeros by

df.time = df.time.str.zfill(4)

>>>df.head()

    time
0   0002
1   1620
2   0155
3   0120
4   0123

Then you can either use apply to map the time_string column like this

df['time_string'] = df.time.apply(lambda x: x[0:2] + ":" + x[2:4])

Or convert it first to datetime and then extract the date string from that object. This might be an unnecessary step for you though - but I like to work time objects as datetime in Python

df['time_datetime'] = df.time.apply(lambda x: datetime.strptime(x,'%H%M'))
df['time_string'] = df.time_datetime.apply(lambda x: x.strftime("%H:%M"))

>>>df.head()

   time       time_datetime             time_string
0  0002       1900-01-01 00:02:00       00:02
1  1620       1900-01-01 16:20:00       16:20
2  0155       1900-01-01 01:55:00       01:55
3  0120       1900-01-01 01:20:00       01:20
4  0123       1900-01-01 01:23:00       01:23

Upvotes: 0

jezrael
jezrael

Reputation: 862731

I think you need timedeltas:

#remove NaNs rows in time column if necessary
#df = df.dropna(subset=['time'])
#or replace NaNs to 0
#df['time1'] = df['time1'].fillna(0)

#convert to int, then str and add 0 
s = df['time'].astype(int).astype(str).str.zfill(4)
#add : twice
df['time1'] = s.str[:2] + ':' + s.str[2:] + ':00'
#convert to timedeltas
df['time2'] = pd.to_timedelta(df['time1'])
print (df)
  bus_no    time     time1    time2
0   Bus1     2.0  00:02:00 00:02:00
1   Bus2   840.0  08:40:00 08:40:00
2   Bus3  2340.0  23:40:00 23:40:00
3   Bus4    15.0  00:15:00 00:15:00
4   Bus5  1205.0  12:05:00 12:05:00
5   Bus6  1304.0  13:04:00 13:04:00
6   Bus7  1620.0  16:20:00 16:20:00
7   Bus8     9.0  00:09:00 00:09:00

Upvotes: 1

Subhrajyoti Das
Subhrajyoti Das

Reputation: 2710

Use:

def get_time(s):
    s = s.replace('.0','')
    time_type = len(s)
    if len(s) == 1:
        return '00:0%s'%s
    elif len(s) == 2:
        return '00:%s'%s
    elif len(s) == 3:
        return '0%s:%s'%(s[0:1], s[1:3])
    elif len(s) == 4:
        return '%s:%s'%(s[0:2], s[2:4])

Upvotes: 0

Yilun Zhang
Yilun Zhang

Reputation: 9018

First, to make your string more consistent, you can use str.zfill(x) to make them of the same length. For example:

"9.0".zfill(6)

will give you "0009.0". Then, you can do something like

df.time.apply(lambda x: x[0:2] + ":" + x[2:4])

to convert it to "HH:MM" format.

Upvotes: 0

Related Questions