10

I have a dataframe like this:

In [67]:
call_df.head()
Out[67]:
timestamp   types
1   2014-06-30 07:00:55 Call_O
2   2014-06-30 07:00:05 Call_O
3   2014-06-30 06:54:55 Call_O
501 2014-06-30 11:24:01 Call_O

When I saved that dataframe to csv file, the format of datetime is change as well as I lose the seconds. I just put this code to save into csv file:

call_df.to_csv('D:/Call.csv')

The csv file output is like this:

enter image description here

In here I want to ask, how to save the same datetime format from dataframe into csv file

3
  • 1
    Have you checked how the raw CSV file looks like? The seconds could be stripped by the tool that you use for viewing it (Excel?). Commented May 18, 2015 at 8:18
  • Aha, yes.. i have seen, how to change that format? Commented May 18, 2015 at 8:27
  • If this is a question about excel formatting, please tag it with excel, if the date format you want in the file is wrong, please look at @EdChum s answer and/or post the raw data from the csv file (open it with notepad, not excel) Commented May 18, 2015 at 9:26

4 Answers 4

15

to_csv accepts a date_format param, so please try this:

call_df.to_csv('D:/Call.csv', date_format='%Y-%m-%d %H:%M:%S')

On your sample data set this yielded:

timestamp,types
2014-06-30 07:00:55,Call_O
2014-06-30 07:00:05,Call_O
2014-06-30 06:54:55,Call_O
2014-06-30 11:24:01,Call_O
Sign up to request clarification or add additional context in comments.

Comments

8

The answer to this problem is a two-step process:

1) Convert the timestamp column to date time first:

call_df['timestamp'] = call_df['timestamp'].astype('datetime64[ns]')

2) Add date_format argument while using the "to_csv" after performing the first step:

call_df.to_csv("Call.csv", date_format='%Y-%m-%d %H:%M:%S')

3 Comments

This answer doesn't add anything new
The point here is that your answer has not added significantly new or different to the existing answers, posting answers that are essentially the same as other answers without anything substantially different just adds noise to SO
@EdChum I added this answer because when I tried to export data to csv by converting the date column to string (as suggested by the other answer), the format while exporting to csv was not as desired, but if you convert timestamp to datetime and then export the dataframe to csv, you will get the desired result. I am sorry if you feel the answer didn't add any value, but the only reason of posting it was to save others from the inconvenience I faced.
4

You problem isn't a python problem and it can't be fix directly. Why?

when you open csv file from excel, it will convert your data to any type it should be in this case your data converted to date type then excel apply default date format to that data. Also, you can't control the date format of excel file since csv files is only a text file, no meta or hidden data to advise excel to proceed.

If you still want to control your format in excel, you need to force your data to be string, not date. most of the case we add ' in front of any data we prefer string format. You will get the correct format display as you want but you lose your date data type.

1 Comment

All of the above didn't work for me. Changing simply to string also didn't work. In my case, Excel forces to have slashes (e.g. 7/1/2001). But the only thing that worked for me is to add single quote front and back.
1

Change your datetime columns to string by :

    call_df['timestamp'] = call_df['timestamp'].apply(lambda v: str(v))

then you can save to csv to retain the format

Comments

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.