0

I have a dataset whit two colums, one for date in this format 20190313 and I convert to date time with this code:

import pandas as pd
from functools import reduce
pd.set_option("display.max_columns", 500)
df['Date_O'] = pd.to_datetime(df.Date_O)

This transform the string into: 2019-03-28

but for the time column I have rows with nine characters like this: 130928487 and eight character rows like this: 91957277.

How I can transform that column into a readable time format? I Tried already with datetime

2
  • Can you add 130928487 indicates which format? Commented Mar 29, 2019 at 4:47
  • That indicates the hour whit milisecons and I want to tansform to a H:M format Commented Mar 29, 2019 at 4:54

1 Answer 1

1

To convert your string to datetime :

from datetime import datetime

# input : 130928487 means 13:09:28:487
input = "130928487"

date_time = datetime.strptime(input, "%H%M%S%f")

print("Date time:", date_time)

Date time: 1900-01-01 13:09:28.487000

d = date_time.strftime("%H:%M")
print("H:M format output:", d)

H:M format ouput : 13:09

Sign up to request clarification or add additional context in comments.

3 Comments

my time columns isn't a timestamp is the hour without format
E.G. '130928487' means 13:09:28:487 and '91957277' means 9:19:57:277
OK, corrected with your comments. With this code, '91957277' gives '1900-01-01 09:19:57.277000' for date_time and '09:19' for H:M format.

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.