Python Forum
how to retain time format in df.to_csv
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to retain time format in df.to_csv
#1
Hi,
I have below data in csv file, and I read the file and rewrite. When write to csv using df.to_csv, it lossing time format, I want to show full time format (yyyy/mm/dd hh:mm:ss). How to define the time formart.

The time in csv file showing (when open) in the form: mm:ss.0, but when I click on particular cell the time format is
mm:ss.o

Name	Age	  date_in	        date_out
A	   23	  2020/08/07 20:12	2020/08/07 20:45
B	   28	  2020/08/11 20:12	2020/08/11 20:42
but it showing (unless I click on cell to see full time format)

Name   Age	    date_in	  date_out
A	   23	    20:12	  20:45
B	   28	    20:12	  20:42
But I want to write in to file an dshow it in fie whne open:

Name	Age	  date_in	        date_out
A	   23	  2020/08/07 20:12	2020/08/07 20:45
B	   28	  2020/08/11 20:12	2020/08/11 20:42
I use below code:

import pandas as pd
df = pd.read_csv(r'PythonCodes\timeForm2.csv')
df.dropna(how='any',inplace=True)
df.to_csv('timeFormOut2.csv',index=False)
Reply
#2
You need to define date_format parameter in .to_csv method. See docs to construct an appropriate format string.
Reply
#3
because you speak of click on cell - note that csv format has no concept of cell. It's plain text file. I guess you just view the csv file in Excel/some spreadsheets program. To see what you really have in your file - open the csv file in editor like notepad or notepad++. Presentation in excel may be affected by many factors
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python date format changes to date & time 1418 4 518 Jan-20-2024, 04:45 AM
Last Post: 1418
  df.to_csv('saved_data.csv') Led_Zeppelin 1 1,102 Sep-11-2022, 12:29 AM
Last Post: deanhystad
  What time format is this? wrybread 3 2,003 Jun-15-2022, 02:46 PM
Last Post: snippsat
Smile Set 'Time' format cell when writing data to excel and not 'custom' limors 3 6,204 Mar-29-2021, 09:36 PM
Last Post: Larz60+
Lightbulb 'str' object has no attribute 'to_csv' brunolelli 3 13,045 Mar-25-2021, 06:40 AM
Last Post: ndc85430
  ValueError: time data 'None' does not match format '%Y-%m-%dT%H:%M:%S.%f' rajesh3383 4 14,202 Sep-03-2020, 08:22 PM
Last Post: buran
  Convert Excel to CSV and retain formatting slider09 5 3,058 Jul-24-2020, 10:36 AM
Last Post: Gribouillis
  getting error ValueError: time data '' does not match format '%H:%M' srisrinu 2 5,549 Apr-09-2020, 11:12 AM
Last Post: srisrinu
  How to define index column name using df.to_csv SriRajesh 0 1,741 Feb-13-2020, 03:45 PM
Last Post: SriRajesh
  Incorrect time format KoSik 5 2,941 Aug-15-2019, 05:10 PM
Last Post: KoSik

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020