user2242044
user2242044

Reputation: 9233

Pandas to_csv makes timestamps into tuples

I have a dataframe with a column of timestamps that I'd like to write to a CSV file, but running into some problems as it seems to be outputting a tuple.

print df['mydate'].loc[0], type(df['mydate'].loc[0])

yields

2009-01-01 00:00:00 <class 'pandas.tslib.Timestamp'>

When I use df.to_csv(), the timestamps are written as a tuple and thus not recognized by Excel as a date.

What it looks like in the csv file:

(Timestamp('2015-02-25 00:00:00'),)

Upvotes: 0

Views: 272

Answers (1)

Ken Wei
Ken Wei

Reputation: 3130

If your goal is to get a date that is recognised by Excel, try doing df['mydate'] = df.mydate.dt.date.astype(str) before saving it as a csv.

If you wanted the time too, you can leave out the .dt.date part. For completeness, you can use .dt.strftime to get things according to some custom format, although this is probably not necessary in this particular case.

Upvotes: 2

Related Questions