问题描述
这是我存储在 df1 中的每日 ohlc 数据的小样本。
#spring.resources.static-locations= classpath:/src/main/webapp/
#spring.resources.static-locations=classpath:/custom/
# both of these commented as none were working for me
#Support for JSP Pages
spring.mvc.view.prefix: /WEB-INF/views/
spring.mvc.view.suffix: .jsp
我想创建一个数据框 (df2),它表示活动年份蜡烛在进展过程中的样子。收盘价为当天收盘价,最高价为1月1日至当天的最高价,最低价为1月1日至当天的最低价,开盘价为当年开盘价。应该是这样的:
<img src="<c:url value='resources/static/StaticImages/mainPage.jpg'/>" title="" alt="">
很想放一些代码,但我在这里迷路了,我认为重新采样会对我有帮助,但它只是将整年总结为一行数据。我也想认为我可以通过每天的迭代和重新采样来解决这个问题,但我知道这会大大减慢计算速度,所以我希望看看这是否可以通过矢量化实现。这是我第一次发帖,所以如果有任何我需要改进的指导方针,请告诉我。
---------------编辑-------------------
这是我的完整代码,年份有效,但其他时间框架无效,希望在我从公共来源 yfinance 中提取数据时,可以更轻松地复制不良结果。
date open close high low
2019-01-01 00:00:00 3700 3800 3806 3646
2019-01-02 00:00:00 3800 3857 3880 3750
2019-01-03 00:00:00 3858 3766 3863 3729
2019-01-04 00:00:00 3768 3791 3821 3706
2019-01-05 00:00:00 3789 3772 3839 3756
2019-01-06 00:00:00 3776 3988 4023 3747
2019-01-07 00:00:00 3985 3972 4018 3928
解决方法
# set date as the index
df = df.set_index('date')
# high is the max from Jan1 to current day
df['max'] = df.groupby(df.index.year)['max'].cummax()
# low is the min from Jan1 to current day
df['min'] = df.groupby(df.index.year)['min'].cummin()
# open is based on the open of the year
for ind,row in df.iterrows():
row['open'] = df.loc[ind.replace(month=1,day=1),'open']
# OPTIONAL: reset index
df = df.reset_index()