β±οΈ Pandas Working with Time Series β Analyze, Index, and Resample Temporal Data
π§² Introduction β Why Work with Time Series in Pandas?
Pandas makes time series analysis simple and powerful by offering native support for datetime indexing, frequency conversion, time-based selection, resampling, and shifting. Itβs the go-to tool for working with temporal data in finance, IoT, web analytics, and forecasting.
π― In this guide, youβll learn:
- How to create and parse datetime indexes
- Time-based slicing and filtering
- Resample and aggregate over periods
- Handle missing time data and perform date arithmetic
π₯ 1. Create a Time Series DataFrame
import pandas as pd
dates = pd.date_range(start='2023-01-01', periods=6, freq='D')
df = pd.DataFrame({'Sales': [100, 120, 130, 90, 110, 150]}, index=dates)
π Output:
Sales
2023-01-01 100
2023-01-02 120
2023-01-03 130
2023-01-04 90
2023-01-05 110
2023-01-06 150
βοΈ The index is a DatetimeIndex, enabling time-aware operations.
ποΈ 2. Convert Column to DateTime and Set Index
df = pd.DataFrame({
'Date': ['2023-01-01', '2023-01-02', '2023-01-03'],
'Revenue': [200, 220, 210]
})
df['Date'] = pd.to_datetime(df['Date'])
df.set_index('Date', inplace=True)
π 3. Time-Based Indexing and Filtering
df['2023-01-02'] # Select data for a specific date
df['2023-01-01':'2023-01-03'] # Select a date range
df.loc[df.index.month == 1] # Filter by month
βοΈ Slice like a boss using natural date formats.
π 4. Resample Time Series Data
df.resample('W').mean() # Weekly average
df.resample('M').sum() # Monthly total
βοΈ resample()
is like groupby()
for time-based aggregation.
π§± 5. Generate Time Ranges and Frequencies
pd.date_range(start='2023-01-01', end='2023-01-10', freq='B') # Business days
pd.date_range(periods=12, freq='M', start='2023-01-01') # Monthly periods
π§ Common frequency aliases:
'D'
: day'B'
: business day'H'
: hour'W'
: week'M'
: month'Q'
: quarter'Y'
: year
βͺ 6. Shift and Lag Data
df.shift(1) # Shift values down (lag)
df.shift(-1) # Shift values up (lead)
df.tshift(1, freq='D') # Shift timestamps (deprecated in newer versions)
βοΈ Useful in trend comparison and lag feature creation.
π§Ό 7. Fill or Interpolate Missing Dates
df = df.asfreq('D') # Force daily frequency
df.fillna(method='ffill') # Forward fill
df.interpolate(method='linear') # Interpolation
βοΈ Perfect for sensor data, web traffic, or irregular time intervals.
π§ͺ 8. Rolling and Window Functions
df.rolling(window=3).mean()
df.expanding().sum()
βοΈ Enables moving averages, rolling sums, and trend smoothing.
π Summary β Key Takeaways
Pandas provides rich, intuitive tools for working with time series dataβmaking it simple to analyze, resample, and clean temporal datasets.
π Key Takeaways:
- Use
pd.date_range()
andto_datetime()
to handle dates - Set a
DatetimeIndex
for time-aware slicing and filtering - Use
resample()
for time-based aggregation - Fill or interpolate gaps with
.asfreq()
,.fillna()
,.interpolate()
- Use
.rolling()
and.shift()
for moving windows and lags
βοΈ Real-world relevance: Critical in financial modeling, sales forecasting, energy monitoring, web analytics, and machine learning pipelines.
β FAQs β Pandas Time Series Handling
β Whatβs the difference between resample()
and groupby()
?resample()
is time-aware and uses time frequency rules, while groupby()
groups by categorical values or custom keys.
β How do I handle missing time periods?
Use .asfreq()
to align to a regular frequency, then fill gaps using .fillna()
or .interpolate()
.
β Can I plot time series directly?
Yes:
df.plot()
Pandas integrates well with Matplotlib for datetime-aware plots.
β How do I detect time gaps in irregular data?
Use:
df.index.to_series().diff().value_counts()
Share Now :