, dtype='datetime64[ns]', freq=None)] 在 [index]",python,pandas,dataframe,pivot,sentiment-analysis"/>

首页 > 解决方案 > alphalens.utils 返回 KeyError: "None of [DatetimeIndex ([', dtype='datetime64[ns]', freq=None)] 在 [index]"

问题描述

我正在尝试对 10K 财务报表进行情绪分析,但使用以下操作时遇到错误:

data = al.utils.get_clean_factor_and_forward_returns(cs_df.stack(), pricing.loc[cs_df.index], quantiles=5, bins=None, periods=[1])

当我返回以下 KeyError print(data)

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(), pricing.loc[cs_df.index], quantiles=5, bins=None, periods=[1])
        factor_data[sentiment] = data

    except:
        skipped_sentiments.append(sentiment)

运行时,它只会打印我所有的情绪,就像它跳到except:for 循环的部分一样。

我的 DataFramecs_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.utilswherepricing.loc[cs_df.index]更改为pricing.loc[cs_df.date]line 无济于事,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操作,并且我认为枢轴不会导致问题。有任何想法吗?感谢您的帮助和时间!

FULL TRACEBACK
---
Traceback (most recent call last):
  File "~FinancialReportSentimentAnalysis.py", line 514, in <module>
    print(al.utils.get_clean_factor_and_forward_returns(cs_df.stack(), pricing.loc[cs_df.index], quantiles=5, bins=None, periods=[1]))
  File "~opt/anaconda3/envs/workspace/lib/python3.8/site-packages/pandas/core/indexing.py", line 879, in __getitem__
    return self._getitem_axis(maybe_callable, axis=axis)
  File "~opt/anaconda3/envs/workspace/lib/python3.8/site-packages/pandas/core/indexing.py", line 1099, in _getitem_axis
    return self._getitem_iterable(key, axis=axis)
  File "~opt/anaconda3/envs/workspace/lib/python3.8/site-packages/pandas/core/indexing.py", line 1037, in _getitem_iterable
    keyarr, indexer = self._get_listlike_indexer(key, axis, raise_missing=False)
  File "~opt/anaconda3/envs/workspace/lib/python3.8/site-packages/pandas/core/indexing.py", line 1254, in _get_listlike_indexer
    self._validate_read_indexer(keyarr, indexer, axis, raise_missing=raise_missing)
  File "~opt/anaconda3/envs/workspace/lib/python3.8/site-packages/pandas/core/indexing.py", line 1298, in _validate_read_indexer
    raise KeyError(f"None of [{key}] are in the [{axis_name}]")
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]', name='date', freq=None)] are in the [index]"

Process finished with exit code 1

标签: pythonpandasdataframepivotsentiment-analysis

解决方案


对于任何偶然发现这一点的人。

问题在于将财务文件中的日期与定价数据表中的日期匹配。当我试图匹配一年中的第一天(元旦)和市场关闭并且这一天没有定价数据时,不可能匹配。因此,我将数据更改为每月数据,以获取 1 月 1 日 YYYY 的汇总价格数据。

pricing.loc[cs_df.index]是问题的根源。


推荐阅读