Search code examples
pythonpandasstring-to-datetime

In python pandas, how can I convert this formatted date string to datetime


I have tried several ways of using to_datetime, but so far I can only get it to return the dtype as "object"

pd.to_datetime(pd.Series(['28Dec2013 19:23:15']),dayfirst=True)

The return from this command is:

0    28Dec2013 19:23:15
dtype: object

Solution

  • You can pass a format parameter to the to_datetime function.

    >>> import pandas as pd
    >>> df = pd.to_datetime(pd.Series(['28Dec2013 19:23:15']),format="%d%b%Y %H:%M:%S",dayfirst=True)
    >>> df
    0   2013-12-28 19:23:15
    dtype: datetime64[ns]