user3042850
user3042850

Reputation: 317

Formatting Time in Python by Concatenating Columns

I have a Pandas data frame with 3 columns: "Hour", "Minute", & "Second" that I need to concatenate into time.

I am hoping to use a date later in my analysis but but for the time being I'm creating a temp column called time. My problem is that the hour, minutes & seconds have either 1 or 2 digits, when there are 2, I want to use the number but if there's 1 [1-9], then I want to write "01" or "02" or ... or "09". Heres's my code

dfdate['AgentDateTime'] = 
(dfdate['DISPATCH_HOUR'] if (len(dfdate['DISPATCH_HOUR'])==2) else ("0"+dfdate['DISPATCH_HOUR'])) 
+ ":" 
+ (dfdate['DISPATCH_MIN'] if (len(dfdate['DISPATCH_MIN'])==2) else ("0"+dfdate['DISPATCH_MIN'])) 
+ ":" 
+ (dfdate['DISPATCH_SEC'] if (len(dfdate['DISPATCH_SEC'])==2) else ("0"+dfdate['DISPATCH_SEC']))

For whatever reason, 10 is being returned, 010, and 11 as 011 and so on. How do I fix this, because when I query an individual line with a double digit number,

len(dfdate['DISPATCH_MIN'])==2 

returns True meaning the if statement's above should return the row without the 0 prefixed to it.

Upvotes: 1

Views: 35

Answers (1)

cs95
cs95

Reputation: 402824

There's no need for that. Just slice out your columns, join them with agg, and then pass the result to pd.to_timedelta.

v = dfdate[['DISPATCH_HOUR', 'DISPATCH_MIN', 'DISPATCH_SEC']]
dfdate['AgentDateTime'] = pd.to_timedelta(
    v.astype(str).agg(':'.join, axis=1), errors='coerce')

Upvotes: 1

Related Questions