1

Let's say:

>>> print(df)

    location        date
    paris     23/02/2010
    chicago    3-23-2013
    ...
    new york  04-23-2013
    helsinki  13/10/2015

Currently, df["date"] is in str. I want to convert the date column to datetime using

>>> df["date"] = pd.to_datetime(df["date"])

I would get ValueError due to ParserError. This is because the format of the date is inconsistent (i.e. dd/mm/yyyy, then next one is m/dd/yyyy).

If I were to write the code below, it still wouldn't work due to the date not being uniformed and delimiters being different:

>>> df["date"] = pd.to_datetime(df["date"], format="%d/%m/%Y")

The last option that I could think of was to write the code below, which replaces all of the dates that are not formatted like the first date to NaT:

>>> df["date"] = pd.to_datetime(df["date"], errors="coerce")

How do I convert the whole date column to datetime while having the dates not uniform in terms of the delimiters, and the orders of days, months and years?

1 Answer 1

1

use, apply method of pandas

df['date'] = df.apply(lambda x: pd.to_datetime(x['date']),axis = 1)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.