pythonpython-3.xpandasdatetimestring-to-datetime

Pandas DateTime String/List


I need to convert a list of strings to date time objects, particularly with Pandas. I really hope this doesn't get flagged as a duplicate because I have seen similar questions, but none have answered my question

I have tried this and I was expecting a value like 'Hello World' to return as NaT, but the only one that does not return as NaT is '14/11/2020', which returned as 2020-11-14.

s = pd.Series(["14 Nov 2020", "14/11/2020", "2020/11/14", 
          "Hello World", "Nov 14th, 2020"])
pd.to_datetime(s, errors='coerce', yearfirst=True, format='%Y-%m-%d', exact=False)

How do I fix this so that mostly all the values return as date times


Solution

  • Just by getting rid of the yearfirst and format arguments I get the output:

    0   2020-11-14
    1   2020-11-14
    2   2020-11-14
    3          NaT
    4   2020-11-14
    dtype: datetime64[ns]