Python - Pandas Dataframe - data not matching source

前提是你 提交于 2019-12-02 10:49:20

To get the average daily up/down price moves, you could:

from pandas_datareader.data import DataReader

data = DataReader('ATVI', "yahoo", datetime(2015, 1, 1), datetime(2015, 12, 31))[['Open', 'Close']]
open = data.Close.resample('M').first() # get the open of the first day, assign date of last day of month
close = data.Close.resample('M').last()  # get the close of the last day, assign date of last day of month
returns = close.subtract(open).div(open) # calculate returns

to get:

Date
2014-01-31   -0.052020
2014-02-28    0.134232
2014-03-31    0.047131
2014-04-30   -0.032866
2014-05-31    0.040561
2014-06-30    0.081474
2014-07-31   -0.007539
2014-08-31    0.049020
2014-09-30   -0.124263
2014-10-31   -0.031083
2014-11-30    0.066503
2014-12-31   -0.042755
2015-01-31    0.038251
2015-02-28    0.103644
2015-03-31   -0.022366
2015-04-30    0.017387
2015-05-31    0.095879
2015-06-30   -0.046850
2015-07-31    0.042863
2015-08-31    0.121865
2015-09-30    0.108758
2015-10-31    0.124919
2015-11-30    0.089384
2015-12-31    0.003630
Freq: M, Name: Close, dtype: float64

To get the mean by months, you can:

returns.groupby(returns.index.month).mean()

to get:

1    -0.006884
2     0.118938
3     0.012383
4    -0.007739
5     0.068220
6     0.017312
7     0.017662
8     0.085442
9    -0.007752
10    0.046918
11    0.077943
12   -0.019563
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!