将日期转换成周几的形式,统计一个月内按照周几的形式分的股票的收盘价的平均值
import numpy as np import matplotlib.pyplot as mp import datetime as dt import matplotlib.dates as md ''' 时间处理:按照周一,周二...分组统计平均值 ''' # 日期转化函数 def dmy2wday(dmy): # 把dmy格式的字符串转化成周几返回 dmy = str(dmy, encoding='utf-8') d = dt.datetime.strptime(dmy, '%d-%m-%Y') d = d.date() wday = d.weekday() # 返回周几 return wday wdays, opening_prices, highest_prices, lowest_prices, closing_prices, volumns = \ np.loadtxt('./da_data/aapl.csv', delimiter=',', usecols=(1, 3, 4, 5, 6, 7), unpack=True, dtype='f8, f8, f8, f8, f8, f8', converters={1: dmy2wday}) # converters为转换器,运行时先执行,其中1表示时间所在的列索引号 print(wdays) ave_prices = np.zeros(5) # 存储最终结果 for wday in range(5): ave_prices[wday] = closing_prices[wdays == wday].mean().round(2) # 保留小数点几位 print(ave_prices)