Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas save date in ISO format?

I'm trying to generate a Pandas DataFrame where date_range is an index. Then save it to a CSV file so that the dates are written in ISO-8601 format.

import pandas as pd
import numpy as np
from pandas import DataFrame, Series

NumberOfSamples = 10
dates = pd.date_range('20130101',periods=NumberOfSamples,freq='90S')
df3 = DataFrame(index=dates)
df3.to_csv('dates.txt', header=False)

The current output to dates.txt is:

2013-01-01 00:00:00
2013-01-01 00:01:30
2013-01-01 00:03:00
2013-01-01 00:04:30
...................

I'm trying to get it to look like:

2013-01-01T00:00:00Z
2013-01-01T00:01:30Z
2013-01-01T00:03:00Z
2013-01-01T00:04:30Z
....................
like image 440
Borisw37 Avatar asked Feb 25 '15 20:02

Borisw37


1 Answers

Use datetime.strftime and call map on the index:

In [72]:

NumberOfSamples = 10
import datetime as dt
dates = pd.date_range('20130101',periods=NumberOfSamples,freq='90S')
df3 = pd.DataFrame(index=dates)
df3.index = df3.index.map(lambda x: dt.datetime.strftime(x, '%Y-%m-%dT%H:%M:%SZ'))
df3
Out[72]:
Empty DataFrame
Columns: []
Index: [2013-01-01T00:00:00Z, 2013-01-01T00:01:30Z, 2013-01-01T00:03:00Z, 2013-01-01T00:04:30Z, 2013-01-01T00:06:00Z, 2013-01-01T00:07:30Z, 2013-01-01T00:09:00Z, 2013-01-01T00:10:30Z, 2013-01-01T00:12:00Z, 2013-01-01T00:13:30Z]

Alternatively and better in my view (thanks to @unutbu) you can pass a format specifier to to_csv:

df3.to_csv('dates.txt', header=False, date_format='%Y-%m-%dT%H:%M:%SZ')
like image 165
EdChum Avatar answered Sep 17 '22 18:09

EdChum