Working with Dates
Turn text into real dates with pd.to_datetime. Then the .dt accessor gives you the year, month, weekday and more.
Time is part of almost every analysis: trends by month, weekday patterns, growth versus last period. Once a column is a real datetime, pandas can extract parts, filter ranges and resample to any period with one line.
Example
Monthly totals with resample
Example
Date maths and ranges
Example
Indian files often write dates as day/month/year (
03/01/2026 = 3 January). Tell pandas with pd.to_datetime(col, dayfirst=True) or a format like format="%d/%m/%Y", or months and days get mixed up.Key points
- to_datetime converts text to dates (watch dayfirst).
- .dt gives year, month, weekday and more.
- resample groups by time periods.
📝 Test yourself
1. Which accessor gives date parts like month and weekday?
2. What does resample("MS") do on a date index?
3. Fill in the option for day/month/year dates:
pd.to_datetime(s, =True)
