How do I convert dates into ISO-8601 DateTime format in a Pandas dataframe
Question:
I have the dataframe below (using python/pandas) and wish to convert the
q_string q_visits q_date
red 1790 02/10/2012 00:00
blue 364 02/10/2012 00:00
current 280 02/10/2012 00:00
molecular 259 02/10/2012 00:00
cell 201 02/10/2012 00:00
How can I convert the ‘q_date’ field into SO-8601 DateTime format (yyyy-MM- ddTHH:mm:ssZ)?
Thanks in advance.
Answers:
Use the pandas datetools parser to parse the date and then format it using the standard python strftime
function.
>>> df['q_date'].apply(
lambda x: pd.datetools.parse(x).strftime('%Y-%m-%dT%H:%M:%SZ'))
0 20120210T00:0000Z
1 20120210T00:0000Z
2 20120210T00:0000Z
3 20120210T00:0000Z
4 20120210T00:0000Z
Name: q_date, dtype: object
First convert your q_date
column into a datetime64[ns]
Series, then map
over the column with a custom format string
In [178]: df = df.convert_objects(convert_dates='coerce')
In [179]: df
Out[179]:
q_string q_visits q_date
0 red 1790 2012-02-10 00:00:00
1 blue 364 2012-02-10 00:00:00
2 current 280 2012-02-10 00:00:00
3 molecular 259 2012-02-10 00:00:00
4 cell 201 2012-02-10 00:00:00
In [180]: df['iso_q_date'] = df.q_date.map(lambda x: datetime.datetime.strftime(x, '%y%m%dT%H:%M%SZ'))
In [181]: df
Out[181]:
q_string q_visits q_date iso_q_date
0 red 1790 2012-02-10 00:00:00 120210T00:0000Z
1 blue 364 2012-02-10 00:00:00 120210T00:0000Z
2 current 280 2012-02-10 00:00:00 120210T00:0000Z
3 molecular 259 2012-02-10 00:00:00 120210T00:0000Z
4 cell 201 2012-02-10 00:00:00 120210T00:0000Z
I would use pd.to_datetime
and the .dt accessor
pd.to_datetime(df['q_date']).dt.strftime('%Y-%m-%dT%H:%M:%SZ')
use to_datetime for pandas datetime format and use strftime to convert to required format.
currentDate = pd.to_datetime(df['q_date'])
convertDate = currentDate.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
or
use isoformat to convert to ISO format.
convertDate = currentDate.isoformat()
To retain the time as well rather than just the date, use pd.Timestamp.isoformat
:
df['q_date'] = pd.to_datetime(df['q_date']).apply(pd.Timestamp.isoformat) + 'Z'
FYI, the timespec
argument allows to specify additional terms of the time to include.
I have the dataframe below (using python/pandas) and wish to convert the
q_string q_visits q_date
red 1790 02/10/2012 00:00
blue 364 02/10/2012 00:00
current 280 02/10/2012 00:00
molecular 259 02/10/2012 00:00
cell 201 02/10/2012 00:00
How can I convert the ‘q_date’ field into SO-8601 DateTime format (yyyy-MM- ddTHH:mm:ssZ)?
Thanks in advance.
Use the pandas datetools parser to parse the date and then format it using the standard python strftime
function.
>>> df['q_date'].apply(
lambda x: pd.datetools.parse(x).strftime('%Y-%m-%dT%H:%M:%SZ'))
0 20120210T00:0000Z
1 20120210T00:0000Z
2 20120210T00:0000Z
3 20120210T00:0000Z
4 20120210T00:0000Z
Name: q_date, dtype: object
First convert your q_date
column into a datetime64[ns]
Series, then map
over the column with a custom format string
In [178]: df = df.convert_objects(convert_dates='coerce')
In [179]: df
Out[179]:
q_string q_visits q_date
0 red 1790 2012-02-10 00:00:00
1 blue 364 2012-02-10 00:00:00
2 current 280 2012-02-10 00:00:00
3 molecular 259 2012-02-10 00:00:00
4 cell 201 2012-02-10 00:00:00
In [180]: df['iso_q_date'] = df.q_date.map(lambda x: datetime.datetime.strftime(x, '%y%m%dT%H:%M%SZ'))
In [181]: df
Out[181]:
q_string q_visits q_date iso_q_date
0 red 1790 2012-02-10 00:00:00 120210T00:0000Z
1 blue 364 2012-02-10 00:00:00 120210T00:0000Z
2 current 280 2012-02-10 00:00:00 120210T00:0000Z
3 molecular 259 2012-02-10 00:00:00 120210T00:0000Z
4 cell 201 2012-02-10 00:00:00 120210T00:0000Z
I would use pd.to_datetime
and the .dt accessor
pd.to_datetime(df['q_date']).dt.strftime('%Y-%m-%dT%H:%M:%SZ')
use to_datetime for pandas datetime format and use strftime to convert to required format.
currentDate = pd.to_datetime(df['q_date'])
convertDate = currentDate.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
or
use isoformat to convert to ISO format.
convertDate = currentDate.isoformat()
To retain the time as well rather than just the date, use pd.Timestamp.isoformat
:
df['q_date'] = pd.to_datetime(df['q_date']).apply(pd.Timestamp.isoformat) + 'Z'
FYI, the timespec
argument allows to specify additional terms of the time to include.