2

I have a Pandas dataframe that has a column of time in string format as below:

 id       playTime

 12       1d 13h 23m
 13      42d 22h 47m
 14       7d 16h 31m
 15        1h 26m 6s 
 16           9d 58m

I tried pd.to_datetime(df['playTime'], format='%j%H%M%S') but it shows time data does not match format '%j%H%M%S' (match). In the end, I'd like to convert thedf['playTime']values to seconds.

1
  • Your question doesn't make sense, what you have is a collection of time durations, there is nothing here to indicate a date. For instance what should the first row represent? Commented Sep 25, 2017 at 12:32

2 Answers 2

1

I think need to_timedelta for Timedeltas with total_seconds:

df['playTime'] = pd.to_timedelta(df['playTime']).dt.total_seconds().astype(int)
print (df)
   id  playTime
0  12    134580
1  13   3710820
2  14    664260
3  15      5166
4  16    781080
Sign up to request clarification or add additional context in comments.

Comments

0

Can you try this:

pd.to_datetime(df['playTime'], format='%j %H %M %S')

Comments

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.