Fundamentals 6 min read

Processing Time Series Data with Pandas: Types, Slicing, Resampling, and Rolling Windows

This tutorial teaches how to handle time series data using pandas, covering datetime types, slicing by date ranges or months, various resampling frequencies, and rolling window calculations such as moving averages and standard deviations, followed by a practical stock price analysis example.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Processing Time Series Data with Pandas: Types, Slicing, Resampling, and Rolling Windows

Objective : Learn to process time series data.

Learning Content : datetime types, time‑series slicing, and resampling.

Code Examples :

import pandas as pd
import matplotlib.pyplot as

# Create example time series dataset
data = {
    '日期': pd.date_range(start='2023-01-01', periods=100, freq='D'),
    '收盘价': [100 + i * 0.5 + (i % 10) * 2 for i in range(100)]
}
df = pd.DataFrame(data)
df.set_index('日期', inplace=True)
print(f"示例时间序列数据集: \n{df.head()}")

# Check datetime type
print(f"日期列的数据类型: {df.index.dtype}")

# Convert string to datetime
df_str = pd.DataFrame({
    '日期': ['2023-01-01', '2023-01-02', '2023-01-03'],
    '收盘价': [100, 101, 102]
})
df_str['日期'] = pd.to_datetime(df_str['日期'])
df_str.set_index('日期', inplace=True)
print(f"字符串转换为日期时间后的数据集: \n{df_str}")

# Slice by date range
df_slice = df['2023-01-01':'2023-01-10']
print(f"按日期范围切片后的数据集: \n{df_slice}")

# Slice by month
df_month_slice = df['2023-01']
print(f"按月份切片后的数据集: \n{df_month_slice}")

# Resample daily
df_resample_daily = df.resample('D').mean()
print(f"按天重采样后的数据集: \n{df_resample_daily.head()}")

# Resample weekly
df_resample_weekly = df.resample('W').mean()
print(f"按周重采样后的数据集: \n{df_resample_weekly.head()}")

# Resample monthly
df_resample_monthly = df.resample('M').mean()
print(f"按月重采样后的数据集: \n{df_resample_monthly.head()}")

# Resample quarterly
df_resample_quarterly = df.resample('Q').mean()
print(f"按季度重采样后的数据集: \n{df_resample_quarterly.head()}")

# Rolling window - moving average
df_rolling_mean = df['收盘价'].rolling(window=7).mean()
print(f"计算滚动平均后的数据: \n{df_rolling_mean.head(10)}")

# Rolling window - standard deviation
df_rolling_std = df['收盘价'].rolling(window=7).std()
print(f"计算滚动标准差后的数据: \n{df_rolling_std.head(10)}")

Practice : Analyze a stock price time series.

# Import libraries
import pandas as pd
import matplotlib.pyplot as plt

# Read stock price data
file_path = 'stock_prices.csv'
df = pd.read_csv(file_path, parse_dates=['日期'], index_col='日期')
print(f"原始股票价格数据集: \n{df.head()}")

# Slice by date range
df_slice = df['2023-01-01':'2023-01-31']
print(f"按日期范围切片后的数据集: \n{df_slice}")

# Weekly resample
df_resample_weekly = df.resample('W').mean()
print(f"按周重采样后的数据集: \n{df_resample_weekly.head()}")

# Rolling 7‑day average
df_rolling_mean = df['收盘价'].rolling(window=7).mean()
print(f"计算滚动平均后的数据: \n{df_rolling_mean.head(10)}")

# Plot original price and rolling average
plt.figure(figsize=(10, 6))
plt.plot(df.index, df['收盘价'], label='收盘价', color='b')
plt.plot(df_rolling_mean.index, df_rolling_mean, label='7天滚动平均', color='r')
plt.xlabel('日期')
plt.ylabel('价格 (元)')
plt.title('股票价格及其7天滚动平均')
plt.legend()
plt.grid(True)
plt.show()

Summary : By completing this exercise you should now be able to work with time series data in pandas, including handling datetime types, slicing, resampling at various frequencies, applying rolling window calculations, and visualizing the results.

data analysisresamplingtime seriespandasrolling window
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.