将每日股价转换为每周/每月/每季度/每学期/每年?

2024-09-30 20:19:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将每日价格转换为每周、每月、每季度、每半年、每年,但代码仅在我为一只股票运行时有效。当我向列表中添加另一只股票时,代码崩溃并给出两个错误ValueError:名称的长度必须与多重索引中的级别数匹配。'and'TypeError:其他必须是多重索引或元组列表。'

代码如下:

import yfinance as yf
from pandas_datareader import data as pdr

symbols = ['AMZN', 'AAPL']

yf.pdr_override()

df = pdr.get_data_yahoo(symbols, start = '2014-12-01', end = '2021-01-01')
df = df.reset_index()
df.Date = pd.to_datetime(df.Date)
df.set_index('Date', inplace = True)

res = {'Open': 'first', 'Adj Close': 'last'}

dfw = df.resample('W').agg(res)
dfw_ret = (dfw['Adj Close'] / dfw['Open'] - 1)

dfm = df.resample('BM').agg(res)
dfm_ret = (dfm['Adj Close'] / dfm['Open'] - 1)

dfq = df.resample('Q').agg(res)
dfq_ret = (dfq['Adj Close'] / dfq['Open'] - 1)

dfs = df.resample('6M').agg(res)
dfs_ret = (dfs['Adj Close'] / dfs['Open'] - 1)

dfy = df.resample('Y').agg(res)
dfy_ret = (dfy['Adj Close'] / dfy['Open'] - 1)

print(dfw_ret)
print(dfm_ret)
print(dfq_ret)
print(dfs_ret)
print(dfy_ret)```

This is what the original df prints:

```Adj Close                     Open             
                  AAPL         AMZN        AAPL         AMZN
Date                                                        
2014-12-01   26.122288   326.000000   29.702499   338.119995
2014-12-02   26.022408   326.309998   28.375000   327.500000
2014-12-03   26.317518   316.500000   28.937500   325.730011
2014-12-04   26.217640   316.929993   28.942499   315.529999
2014-12-05   26.106400   312.630005   28.997499   316.799988
...                ...          ...         ...          ...
2020-12-24  131.549637  3172.689941  131.320007  3193.899902
2020-12-28  136.254608  3283.959961  133.990005  3194.000000
2020-12-29  134.440399  3322.000000  138.050003  3309.939941
2020-12-30  133.294067  3285.850098  135.580002  3341.000000
2020-12-31  132.267349  3256.929932  134.080002  3275.000000


And this is what the different df_ret print when I go from daily 
to weekly/monthly/etc but it can only do it for one stock and 
the idea is to be able to do it for multiple stocks:


Date
2014-12-07   -0.075387
2014-12-14   -0.013641
2014-12-21   -0.029041
2014-12-28    0.023680
2015-01-04    0.002176
                ...   
2020-12-06   -0.014306
2020-12-13   -0.012691
2020-12-20    0.018660
2020-12-27   -0.008537
2021-01-03    0.019703
Freq: W-SUN, Length: 318, dtype: float64

Date
2014-12-31   -0.082131
2015-01-30    0.134206
2015-02-27    0.086016
2015-03-31   -0.022975
2015-04-30    0.133512
                ...   
2020-08-31    0.085034
2020-09-30   -0.097677
2020-10-30   -0.053569
2020-11-30    0.034719
2020-12-31    0.021461
Freq: BM, Length: 73, dtype: float64

Date
2014-12-31   -0.082131
2015-03-31    0.190415
2015-06-30    0.166595
2015-09-30    0.165108
2015-12-31    0.322681
2016-03-31   -0.095461
2016-06-30    0.211909
2016-09-30    0.167275
2016-12-31   -0.103026
2017-03-31    0.169701
2017-06-30    0.090090
2017-09-30   -0.011760
2017-12-31    0.213143
2018-03-31    0.234932
2018-06-30    0.199052
2018-09-30    0.190349
2018-12-31   -0.257182
2019-03-31    0.215363
2019-06-30    0.051952
2019-09-30   -0.097281
2019-12-31    0.058328
2020-03-31    0.039851
2020-06-30    0.427244
2020-09-30    0.141676
2020-12-31    0.015252
Freq: Q-DEC, dtype: float64

Date
2014-12-31   -0.082131
2015-06-30    0.388733
2015-12-31    0.538386
2016-06-30    0.090402
2016-12-31    0.045377
2017-06-30    0.277180
2017-12-31    0.202181
2018-06-30    0.450341
2018-12-31   -0.107405
2019-06-30    0.292404
2019-12-31   -0.039075
2020-06-30    0.471371
2020-12-31    0.180907
Freq: 6M, dtype: float64

Date
2014-12-31   -0.082131
2015-12-31    1.162295
2016-12-31    0.142589
2017-12-31    0.542999
2018-12-31    0.281544
2019-12-31    0.261152
2020-12-31    0.737029
Freq: A-DEC, dtype: float64```

Tags: dfclosedateresopenaggresampleprint
1条回答
网友
1楼 · 发布于 2024-09-30 20:19:57

在不知道您的df数据帧看起来像什么的情况下,我假设这是一个在多索引上正确处理重采样的问题,与本question中讨论的类似

这里列出的解决方案是使用pd.Grouper并正确填写freqlevel参数

# This is just from the listed solution so I am not sure if these is the correct level to choose
df.groupby(pd.Grouper(freq='W', level=-1))

如果这不起作用,我认为您需要提供更多的细节或虚拟数据集来重现问题

相关问题 更多 >