Parse dates with pd.to_datetime, extract components with .dt, and filter time ranges. Correct datetime dtypes unlock resampling and rolling windows later.
Parsing and dtype
import pandas as pd
dates = pd.to_datetime(['2024-01-15', '2024-02-01'])
print(dates.dtype)
.dt accessor
s.dt.year,.month,.day,.dayofweeks.dt.strftime('%Y-%m')— format as strings.dt.to_period('M')— monthly period
Filtering by date
df = pd.DataFrame({'date': pd.to_datetime(['2024-01-01','2024-06-01']),
'val': [1, 2]})
q1 = df[df['date'] < '2024-04-01']
print(q1)
Important interview questions and answers
- Q: Timezone aware?
A: Useutc=Trueortz_localizefor global data—avoid ambiguous local times. - Q: to_datetime errors?
A: Useerrors='coerce'for messy date strings like numeric conversion.
Self-check
- Parse a string column to datetime.
- Extract year and month with .dt.
Pitfall: Parse dates with pd.to_datetime before resample/rolling—object strings won't work.
Interview prep
- to_datetime?
Parse strings to datetime64—foundation for resample/rolling.
- .dt accessor?
Extract year, month, dayofweek from datetime Series.