import pandas as pd
import numpy as np
import datetime as dt
import matplotlib.pyplot as plt
import seaborn as sns
###计算股票的月度收益
rn3=pd.read_csv('data2.csv',encoding='gbk',index_col='Dates')
rn3_ret=np.log(rn3.pct_change()+1)#转换为对数收益率,是的收益率可加
rn3_ret.index=[dt.datetime.strptime(i,'%Y/%m/%d').month for i in rn3.index] #将行标改为月份
rn3_MonthlyRet=rn3_ret.groupby(rn3_ret.index).sum()
rn3_MonthlyRet.index.name='月份'
rn3_MonthlyRet.columns.name='股票'
###做热力图
plt.figure(figsize=(10,8))
sns.heatmap(rn3_MonthlyRet, annot=True, linewidth=5)