Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Lost the second of the datetime when exporting to the CSV file?

It will lost the second when exporting to the csv file?
why?
"%S" is none?

df = pd.DataFrame({'date': ['2011-01-01 10:15:20', '2016-11-17 08:22:10'],
'text': ['red', 'purple'],'datetime': ['2011-01-01 10:15:20', '2016-11-17 08:22:10']})
df.to_csv("test.csv",date_format="%Y-%m-%d %H:%M:%S")

In the csv file ,it is:

    date    text    datetime
0   2011/1/1 10:15  red 2011/1/1 10:15
1   2016/11/17 8:22 purple  2016/11/17 8:22

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you open the file in the Excel Application, the seconds would be truncated based on the default format. I would recommend to open the file in text editors like – Notepad++, Sublime where the format is preserved.

I ran the below code as it is,

df = pd.DataFrame({'date': ['2011-01-01 10:15:20', '2016-11-17 08:22:10'],
'text': ['red', 'purple'],'datetime': ['2011-01-01 10:15:20', '2016-11-17 08:22:10']})
df.to_csv("test.csv",date_format="%Y-%m-%d %H:%M:%S")

Below is the output I got when opened in Notepad++

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading