Date & Time Handling
Python · Data Cleaning
Theory
Once a column is datetime64 (via pd.to_datetime or parse_dates), the .dt accessor exposes its parts: .dt.year, .dt.month, .dt.day, .dt.day_name(), .dt.quarter, .dt.hour and .dt.date. .dt.to_period('M') turns dates into months like 2024-01 for monthly grouping, and .dt.strftime('%d-%b-%Y') formats them as text.
Date arithmetic works with Timedelta: df['order_date'] + pd.Timedelta(days=7) gives a due date, and subtracting two date columns gives a Timedelta whose .dt.days is the gap in days. pd.DateOffset(months=1) moves by calendar months. pd.date_range('2024-01-01', periods=7, freq='D') builds a date sequence. resample('ME', on='order_date')['amount'].sum() groups by calendar month end ('W' for weeks, 'D' for days) - like a groupby on a time bucket that also includes empty periods. Comparisons work directly: df[df['order_date'] >= '2024-02-01'].
Why we use it
Most business questions are about time - sales per month, delivery delay in days, orders this week. The .dt accessor and resample turn raw timestamps into these answers without manual string slicing.
When to use it
Convert date text to datetime first, then use .dt for parts, subtraction for durations and resample or to_period for time buckets. For grouping by month alongside other keys, groupby with dt.to_period('M') is often simpler than resample.
How it works
- Date column as text
- Convert with pd.to_datetime(...)
- Need parts of the date? Yes: Use .dt.year / .month / .day_name(). No: Go on.
- Need durations or due dates? Yes: Subtract dates or add pd.Timedelta. No: Go on.
- Bucket by period: dt.to_period('M') or resample('ME')
- Time-based columns and summaries
Syntax
s.dt.year / .month / .day_name() / .quarter
s.dt.to_period('M')
s + pd.Timedelta(days=n); (end - start).dt.days
df.resample('ME', on='date_col')['x'].sum()Example
df['month'] = df['order_date'].dt.to_period('M')
df['days'] = (df['delivered'] - df['order_date']).dt.daysKey points
- Convert first: .dt only works on datetime64 columns
- date2 - date1 gives a Timedelta; .dt.days turns it into a number
- Use 'ME' (month end) in resample; plain 'M' is deprecated in recent pandas
- to_period('M') is ideal for groupby by month
- SQL equivalent: EXTRACT(MONTH FROM d), DATEDIFF('day', a, b), DATE_TRUNC('month', d)