Convert integer in format HHMMSS to datetime in Pandas


I have a long dataframe (35 million rows) of data that includes a Time column. The time in the original CSV is in the format HHMMSS, which translates into pandas as an integer. Being an integer, if the Hours are less than 2 digits the first digit drops off (ex: 090000 (9am) becomes 90000 in pandas).

I am trying to convert these integers into an actual datetime value that I can extract .time() from, so as to be able to count in custom intervals of minutes, seconds, hours etc.

How do I convert an integer (such as 90000 or 100000) into their respective times (9am, 10am)?

Asked By: philbeet



You can use to_datetime after converting to string and zfilling the zeros:

df = pd.DataFrame({'time': [90000, 0, 123456]})

df['time2'] = pd.to_datetime(df['time'].astype(str).str.zfill(6), format='%H%M%S').dt.time

Or, as string:

df['time2'] = pd.to_datetime(df['time'].astype(str).str.zfill(6), format='%H%M%S').dt.strftime('%H:%M:%S')


     time     time2
0   90000  09:00:00
1       0  00:00:00
2  123456  12:34:56
Answered By: mozway

You can convert the values to time (Timedelta) directly when loading through the converters parameter. This parameter tells read_csv how to convert specific columns. Unfortunately, there’s no equivalent to strptime for timedelta.

One quick & dirty way to parse this format into a timedelta is to use a regular expression or just split the string into parts, eg :

def parse_timedelta(time_str):
    return pd.Timedelta(

This can be used to convert fields in HHMMSS format to timedelta:

df = pd.read_csv(StringIO(csv),converters={'Time':parse_time})
>>> df
0 3 days 18:00:00
1 0 days 00:00:00
2 0 days 12:34:56
Answered By: Panagiotis Kanavos
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.