Lesson 27 of 60 · python
Time Series Data with Pandas
Duration: 25 minutes
Time Series with Pandas
Temporal data analysis is a core skill for finance, IoT, and many domains.
Converting strings to datetime
df['date'] = pd.to_datetime(df['date'])
Setting the datetime index
df_ts = df.set_index('date')
print(df_ts.head())
Resampling (e.g., daily to monthly)
monthly = df_ts.resample('M').sum()
print(monthly.head())
Rolling windows
# 7‑day moving average of a metric
df_ts['metric_ma7'] = df_ts['metric'].rolling(window=7).mean()
Time‑zone handling
df['date_utc'] = df['date'].dt.tz_localize('UTC')
df['date_est'] = df['date_utc'].dt.tz_convert('US/Eastern')
Frequency conversion and forward/backward fill
# Forward fill missing daily data
filled = df_ts.asfreq('D', method='ffill')