-
Notifications
You must be signed in to change notification settings - Fork 0
/
f.py
30 lines (27 loc) · 830 Bytes
/
f.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat May 27 17:14:05 2017
@author: skywalk
"""
import tushare as ts
import pandas as pd
import numpy as np
def mmean(dfin):
dcdata=dfin
dfom=pd.DataFrame()
dfomi=0
dctemp=(dcdata.iloc[0].date)[5:7]
for i in dcdata.index :
#print (type(i),i,)
if (dcdata.iloc[i].date)[5:7]!= dctemp :
#print (i,dcdata.iloc[i])
dct5=dcdata.iloc[i]
dfom=dfom.append(pd.DataFrame({'date':dct5.date,'open':dct5.open,'close':dcdata.iloc[i-1].close,'m':dct5.date[5:7]},index=[dfomi]))
dfomi=dfomi+1
dctemp=(dcdata.iloc[i].date)[5:7]
#算出每月的收益率
dfom['mr']=(dfom.shift(-1).close-dfom.open)/dfom.open
#聚合月数据
dct6=dfom.groupby('m')
return (dct6)