简体   繁体   中英

How transform string to readable time format in python?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM