0

I have a dataframe with the following columns:

       0           Pressure [bar] Temperature [C]   
0   13:07:46.380:   30.0911         11.8    
1   13:07:47.332:   30.0940         11.8    
2   13:07:50.998:   30.0911         11.8

I want to convert the first column to a proper datetime, so I've tried:

df2['Time'] = pd.to_datetime(df2[0],format='%H:%M:%S.%f:')

But the result I got was:

    0           Pressure [bar]  Temperature [C] Time
0   13:07:46.380:   30.0911     11.8      1900-01-01 13:07:46.380
1   13:07:47.332:   30.0940     11.8      1900-01-01 13:07:47.332
2   13:07:50.998:   30.0911     11.8      1900-01-01 13:07:50.998

Which is obviously not what I wanted, I want it to be just the time, meaning:

 Pressure [bar] Temperature [C]     Time
0       30.0911     11.8      13:07:46.380
1       30.0940     11.8      13:07:47.332
2       30.0911     11.8      13:07:50.998

And using the Time column as an index.

Any ideas?

Thank you!

2
  • did you try changing the format? pd.to_datetime(df2[0],format='%H:%M:%S') will give you hours, mins and seconds. If you want a unix timestamp use pandas.pydata.org/pandas-docs/stable/reference/api/… Commented Feb 7, 2019 at 21:55
  • What does "proper datetime" mean in this context? You should show us how you want your result to look in an edit. Commented Feb 7, 2019 at 21:55

1 Answer 1

2

Did you try:

  df2['Time'] = pd.to_datetime(df2['Time']).dt.time
Sign up to request clarification or add additional context in comments.

1 Comment

Okay, I've edited your comment df2[0] to df2['Time'] and now it works!

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.