Search code examples
pythonpandasdatedatetimeiso8601

How do I convert dates into ISO-8601 DateTime format in a Pandas dataframe


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.


Solution

  • 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