【发布时间】:2018-06-19 20:50:27
【问题描述】:
我正在尝试编写一段通用代码:
-
对一些价格数据进行时间序列并将其划分为十分位数,例如将过去 1800 万的黄金价格分成十分位数 [DONE,见下文]
date 4. close decile 2017-01-03 1158.2 0 2017-01-04 1166.5 1 2017-01-05 1181.4 2 2017-01-06 1175.7 1 ... ... 2018-04-23 1326.0 7 2018-04-24 1333.2 8 2018-04-25 1327.2 7 [374 rows x 2 columns] -
提取特定十分位数的日期,然后创建一个添加了 30 天的辅助日期列表
#So far only for a single decile at a time firstdecile = gold.loc[gold['decile'] == 1] datelist = list(pd.to_datetime(firstdecile.index)) datelist2 = list(pd.to_datetime(firstdecile.index) + pd.DateOffset(months=1)) -
取每个十分位数的 30 天价格回报的平均值
level1 = gold.ix[datelist] level2 = gold.ix[datelist2] level2.index = level2.index - pd.DateOffset(months=1) result = pd.merge(level1,level2, how='inner', left_index=True, right_index=True) def ret(one, two): return (two - one)/one pricereturns = result.apply(lambda x :ret(x['4. close_x'], x['4. close_y']), axis=1) mean = pricereturns.mean() 在单个 CSV 文件中返回所有 10 个平均值的列表
到目前为止,我已经能够将执行步骤 1-3 的功能组合在一起,但仅适用于单个十分位数,但我正在努力将其扩展为一次干净的所有 10 个十分位数的循环代码CSV 输出
【问题讨论】:
标签: python csv for-loop finance