Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert a string to timetuple in Python

Suppose I have a Dataframe as:

df = {'date':['20170101', '20170102', '20170103', '20170104']}

I want to convert '20170101' to '2017-01-01 00:00:00'

How can I achieve it?

like image 801
xxyao Avatar asked Jul 25 '26 01:07

xxyao


1 Answers

You could convert your date time string to datetime object. And then get whatever format you want:

from datetime import datetime

datetime.strptime("20170103", "%Y%m%d").strftime("%Y-%m-%d %H:%M:%S")

Output:

"2017-01-03 00:00:00"
like image 149
Kevin Avatar answered Jul 26 '26 14:07

Kevin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!