如何解决“ alphalens.utils返回KeyError:” [DatetimeIndex['<dates>,dtype ='datetime64 [ns]',freq = None]都不存在“
我正在尝试对10K财务报表进行情感分析,并且使用以下操作遇到了错误:
data = al.utils.get_clean_factor_and_forward_returns(cs_df.stack(),pricing.loc[cs_df.index],quantiles=5,bins=None,periods=[1])
当我print(data)
时,它返回以下KeyError:
KeyError: "None of [DatetimeIndex(['2012-01-01','2013-01-01','2014-01-01','2015-01-01','2016-01-01','2017-01-01','2018-01-01','2019-01-01'],dtype='datetime64[ns]',freq=None)] are in the [index]"
相关代码块:
import alphalens as al
factor_data = {}
skipped_sentiments = []
for sentiment in sentiments:
cs_df = cosine_similarities_df[(cosine_similarities_df['sentiment'] == sentiment)]
cs_df = cs_df.pivot(index='date',columns='ticker',values='value')
try:
data = al.utils.get_clean_factor_and_forward_returns(cs_df.stack(),periods=[1])
factor_data[sentiment] = data
except:
skipped_sentiments.append(sentiment)
“何时运行”仅将我的所有观点打印为已跳过,就像跳过到for循环的except:
部分一样。
在透视操作后,我的DataFrame cs_df
如下所示:
ticker AMZN
date
2012-01-01 0.99706919
2013-01-01 0.99931987
2014-01-01 0.99841510
2015-01-01 0.99805389
2016-01-01 0.99884780
2017-01-01 0.96819148
2018-01-01 0.99957861
2019-01-01 0.99767198
在for循环中,我尝试使用日期索引创建新列并将其映射到al.utils
,其中pricing.loc[cs_df.index]
更改为pricing.loc[cs_df.date]
行,而使用{{ 1}}
我也尝试过使用以下方法删除列和索引名称:
cs_df['date'] = cs_df.index
重命名列和索引会产生以下数据框,但是仍然返回相同的错误
cs_df.columns.name = None
cs_df.index.name = None
使用 AMZN
2012-01-01 0.99706919
2013-01-01 0.99931987
2014-01-01 0.99841510
2015-01-01 0.99805389
2016-01-01 0.99884780
2017-01-01 0.96819148
2018-01-01 0.99957861
2019-01-01 0.99767198
重置索引也无济于事,只是摆脱了我的日期。
相当确定我使用cs_df = cs_df.reset_index(drop=True)
操作的方式存在问题,并且我认为枢轴不会导致问题。有任何想法吗?感谢您的帮助和时间!
al.utils
解决方法
对于绊脚石的人。
问题在于将财务文档中的日期与定价数据表中的日期匹配。由于我正试图匹配年度的第一天(元旦),并且市场关闭,因此这一天没有定价数据,因此无法匹配。因此,我将数据更改为每月,以获取YYYY 1月1日的汇总价格数据。
pricing.loc[cs_df.index]
是问题的根源。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。