问答交流

【代码报错】代码报错:KeyError: "['group'] not in index"

由bq4n08z8创建,最终由xuxiaoyin 被浏览 13 用户

查询经营现金流量净额增长率报错。代码如下:
alpha_test = {
    "alpha_class":"财务因子",
    "alpha_name":"F116",
    "alpha_name_chinese":"经营现金流量净额增长率",
    "alpha_sql":"""
        SELECT
            date, 
            instrument, 
            ((net_cffoa-m_lag(net_cffoa, 135))/m_lag(net_cffoa, 135)) AS factor
        FROM cn_stock_financial_cashflow_general_pit
        ORDER BY date, instrument
    """,
    "alpha_desc":" ", 
    "group_num":10, 
    "instruments":"沪深300",
    "benchmark":"沪深300", 
    "data_process":True,
    "is_bigvip":False,
    "is_featured":False,
}

报错信息如下:

KeyError Traceback (most recent call last) Cell In[3], line 1 ----> 1 alpha_miner = AlphaMiner(params=alpha_test) Cell In[1], line 32 29 print("2. 个股收益计算完成, 总耗时", round(time.time()-t0, 2), "秒") 31 # 3. 分组收益率与分组累计收益率计算 ---> 32 self.group_ret, self.group_cumret = self.get_group_cumret() 33 print("3. 分组收益计算完成, 总耗时", round(time.time()-t0, 2), "秒") 35 # 4. 综合收益与IC指标计算 Cell In[1], line 138 136 bm_ret = bm_ret.set_index('date') 137 # 分组收益率 --> 138 groupret_data = self.group_data[['date','group','daily_ret']].groupby(['date','group'], group_keys=False).apply(lambda x:np.nanmean(x)).reset_index() 139 groupret_data.rename(columns={0:'g_ret'}, inplace=True) 140 groupret_pivotdata = groupret_data.pivot(index='date', values='g_ret', columns='group') File /opt/pyenv/versions/3.11.8/lib/python3.11/site-packages/pandas/core/frame.py:4096, in DataFrame.getitem(self, key) 4094 if is_iterator(key): 4095 key = list(key) -> 4096 indexer = self.columns._get_indexer_strict(key, "columns")[1] 4098 # take() does not accept boolean indexers 4099 if getattr(indexer, "dtype", None) == bool: File /opt/pyenv/versions/3.11.8/lib/python3.11/site-packages/pandas/core/indexes/base.py:6200, in Index._get_indexer_strict(self, key, axis_name) 6197 else: 6198 keyarr, indexer, new_indexer = self._reindex_non_unique(keyarr) -> 6200 self._raise_if_missing(keyarr, indexer, axis_name) 6202 keyarr = self.take(indexer) 6203 if isinstance(key, Index): 6204 # GH 42790 - Preserve name from an Index File /opt/pyenv/versions/3.11.8/lib/python3.11/site-packages/pandas/core/indexes/base.py:6252, in Index._raise_if_missing(self, key, indexer, axis_name) 6249 raise KeyError(f"None of [{key}] are in the [{axis_name}]") 6251 not_found = list(ensure_index(key)[missing_mask.nonzero()[0]].unique()) -> 6252 raise KeyError(f"{not_found} not in index") KeyError: "['group'] not in index"

标签

数据分析
评论
  • 数据过滤之后就没有数据了,在传入因子之前确保sql能取出数据呢
{link}