2

I am looking to convert raw string to a specified data time format.

Here's the sample data:

0              47 mins
1       1 hour 25 mins
2       1 hour 27 mins
3               6 mins

Is the above one of the supported date time formats in python that can be transformed using some function - to_datetime or strftime? or would this need to handled/parsed out differently.

Expected format:

00:47
01:25
01:27
00:06

2 Answers 2

3

You have 2 potential formats, so you can try them each:

s = pd.Series(['47 mins', '1 hour 25 mins', '1 hour 27 mins', '6 mins'])

dt1 = pd.to_datetime(s, format='%H hour %M mins', errors='coerce')
dt2 = pd.to_datetime(s, format='%M mins', errors='coerce')

res = dt1.fillna(dt2).dt.strftime('%H:%M')

print(res)

0    00:47
1    01:25
2    01:27
3    00:06
dtype: object
Sign up to request clarification or add additional context in comments.

Comments

2

You can convert some keywords to appropriate things to be passed to pandas.Timedelta

d = {'mins': 'minutes', 'secs': 'seconds', 'hour': 'hours'}
td = [
    pd.Timedelta(**dict(zip(s[1::2], map(float, s[::2]))))
    for s in [s.split() for s in s.replace(d, regex=True)]
]

pd.Series(td, s.index)

0   00:47:00
1   01:25:00
2   01:27:00
3   00:06:00
dtype: timedelta64[ns]

2 Comments

Solution looks elegant. If you don't mind, can you add a few comments around the td block.
This solution does look neat :). I have sneaky feeling that repeated built-in str.replace without regex will be more efficient than pd.Series.replace(regex=True).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.