We use to_datetime() to change text or numbers into dates. This helps us work with dates easily in data.
to_datetime() for date parsing in Pandas
pandas.to_datetime(arg, format=None, errors='raise', utc=None, dayfirst=False, yearfirst=False)
arg is the data to convert (like a list, Series, or string).
format helps speed up parsing if you know the date pattern (like '%Y-%m-%d').
import pandas as pd # Convert a single date string date = pd.to_datetime('2024-06-01') print(date)
dates = ['2024-06-01', '2024-07-15', '2024-08-20'] dates_parsed = pd.to_datetime(dates) print(dates_parsed)
dayfirst=True because dates are in day/month/year format.dates = ['01/06/2024', '15/07/2024'] dates_parsed = pd.to_datetime(dates, dayfirst=True) print(dates_parsed)
dates = ['20240601', '20240715'] dates_parsed = pd.to_datetime(dates, format='%Y%m%d') print(dates_parsed)
This code converts a column of date strings into real dates. Then it adds a new column showing the month number.
import pandas as pd # Sample data with dates as strings data = {'event': ['start', 'middle', 'end'], 'date_str': ['2024-06-01', '2024-07-15', '2024-08-20']} df = pd.DataFrame(data) # Convert the 'date_str' column to datetime df['date'] = pd.to_datetime(df['date_str']) # Add a new column for the month df['month'] = df['date'].dt.month print(df)
If a date string is invalid, errors='raise' will stop the program. Use errors='coerce' to get NaT (missing date) instead.
Using format speeds up conversion when you know the exact date pattern.
Remember to check if your dates use day-first or year-first order and set dayfirst or yearfirst accordingly.
to_datetime() changes text or numbers into date objects for easy date work.
You can convert single dates, lists, or whole columns in a DataFrame.
Use options like format and dayfirst to help pandas understand your dates correctly.