Set a datetime index and use resample to change frequency—daily to monthly, hourly to daily—with aggregation rules. Foundation for financial and sensor analytics.
DatetimeIndex
import pandas as pd
import numpy as np
idx = pd.date_range('2024-01-01', periods=6, freq='D')
s = pd.Series(np.arange(6), index=idx)
print(s)
resample
monthly = s.resample('ME').sum() # month end
print(monthly)
Common rules
'D'— daily,'W'— weekly,'ME'— month end.mean(),.sum(),.ohlc()on resampled groupsasfreq— reindex to new frequency without aggregation
Important interview questions and answers
- Q: resample vs groupby?
A: resample requires DatetimeIndex; groupby works on any column including date parts. - Q: Upsampling?
A: Higher frequency creates NaN—use ffill/interpolate to fill.
Self-check
- Create a daily Series with date_range.
- Resample to monthly sum.
Tip: Set datetime index with set_index('date') before resample.
Interview prep
- DatetimeIndex?
Required for resample—set_index on date column first.
- Upsampling?
Higher frequency introduces NaN—ffill or interpolate.