import pandas as pd
stock_day = pd.read_csv("stock_day.csv")
stock_day = stock_day.sort_index()
# 对每日交易数据进行重采样 (频率转换)
stock_day.index# 1、必须将时间索引类型转换成Pandas默认的类型
stock_day.index = pd.to_datetime(stock_day.index)# 2、进行频率转换日K---周K,首先让所有指标都为最后一天的价格
period_week_data = stock_day.resample('W').last()# 分别对于开盘、收盘、最高价、最低价进行处理
period_week_data['open'] = stock_day['open'].resample('W').first()
# 处理最高价和最低价
period_week_data['high'] = stock_day['high'].resample('W').max()
# 最低价
period_week_data['low'] = stock_day['low'].resample('W').min()
# 成交量 这一周的每天成交量的和
period_week_data['volume'] = stock_day['volume'].resample('W').sum()
做好后: