DEV Community

Cover image for Convert String to Date in Pandas and Python
Softhints
Softhints

Posted on

Convert String to Date in Pandas and Python

If you need to convert string to dates in Python and Pandas you can check:

How to Convert String to DateTime in Pandas

It contains basic date conversion like:

pd.to_datetime(df['date'])
Enter fullscreen mode Exit fullscreen mode

plus extra examples like:

  • custom date and time patterns
  • infer date pattern
  • dates different language locales
  • different date formats
pd.to_datetime(df['date'] , format='%Y%m%d HH:MM:SS', errors='ignore')
Enter fullscreen mode Exit fullscreen mode

Also it will show if a given string contains dates with fuzzy matching.

Or mixed dates in a Pandas column:

    if '/' in date:
        return pd.to_datetime(date, format = '%Y/%m/%d')
    elif '-' in date:
        return pd.to_datetime(date, format = '%d-%m-%Y')
    else:
        return pd.to_datetime(date, format = '%d.%m.%Y')
Enter fullscreen mode Exit fullscreen mode

Top comments (0)