【问题标题】:A squarred variable is outside the index平方变量在索引之外
【发布时间】:2020-08-20 19:14:09
【问题描述】:

这篇文章的一个变体,没有详细的回溯,大约两个小时前在 SO 中发布。此版本包含整个回溯。)

我正在运行 StatsModels 从普通最小二乘法 (OLS) 获取参数估计值。数据处理和特定于模型的命令如下所示。当我使用 import statsmodels.formula.api 作为 smas 操作 api 时,OLS 按需要工作(在我以编程方式删除大约 15 行之后),给出直观的结果。但是,当我切换到 import statsmodels.api as sm 作为绑定 api 时,几乎根本不更改代码,事情就崩溃了,Python 解释器触发了一个错误,指出“inc_2 不在索引中”。请注意,inc_2 是在两次模型运行中将数据帧读入 StatsModels 后计算的:但第一次运行成功,但第二次运行失败。 (顺便说一句,p_c_inc_18 是人均收入,inc_2 是前者的平方。inc_2 是第二轮的进攻要素。)

import pandas as pd 
import numpy as np 
import statsmodels.api as sm 
%matplotlib inline import 
matplotlib.pyplot as plt 
import seaborn as sns 
sns.set(style="whitegrid") eg=pd.read_csv(r'C:/../../../une_edu_pipc_06.csv') pd.options.display.precision = 3 
plt.rc("figure", figsize=(16,8)) 
plt.rc("font", size=14) 
sm_col = eg["lt_hsd_17"] + eg["hsd_17"] 
eg["ut_hsd_17"] = sm_col 
sm_col2 = eg["sm_col_17"] + eg["col_17"] eg["bnd_hsd_17"] = sm_col2 
eg["d_09"]= eg["Rate_09"]-eg["Rate_06"] 
eg["d_10"]= eg["Rate_10"]-eg["Rate_06"] inc_2=eg["p_c_inc_18"]*eg["p_c_inc_18"] 
X = eg[["p_c_inc_18","ut_hsd_17","d_10","inc_2"]] 
y = eg["Rate_18"] 
X = sm.add_constant(X) 
mod = sm.OLS(y, X) 
res = mod.fit()
print(res.summary())

这是完整的回溯。

KeyError                                  Traceback (most recent call last)
<ipython-input-21-e2f4d325145e> in <module>
     17 eg["d_10"]= eg["Rate_10"]-eg["Rate_06"]
     18 inc_2=eg["p_c_inc_18"]*eg["p_c_inc_18"]
---> 19 X = eg[["p_c_inc_18","ut_hsd_17","d_10","inc_2"]]
     20 y = eg["Rate_18"]
     21 X = sm.add_constant(X)

~\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2804             if is_iterator(key):
   2805                 key = list(key)
-> 2806             indexer = self.loc._get_listlike_indexer(key, axis=1, raise_missing=True)[1]
   2807 
   2808         # take() does not accept boolean indexers

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _get_listlike_indexer(self, key, axis, raise_missing)
   1550             keyarr, indexer, new_indexer = ax._reindex_non_unique(keyarr)
   1551 
-> 1552         self._validate_read_indexer(
   1553             keyarr, indexer, o._get_axis_number(axis), raise_missing=raise_missing
   1554         )

~\Anaconda3\lib\site-packages\pandas\core\indexing.py in _validate_read_indexer(self, key, indexer, axis, raise_missing)
   1644             if not (self.name == "loc" and not raise_missing):
   1645                 not_found = list(set(key) - set(ax))
-> 1646                 raise KeyError(f"{not_found} not in index")
   1647 
   1648             # we skip the warning on Categorical/Interval

KeyError: "['inc_2'] not in index"

我做错了什么?

【问题讨论】:

  • 请提供预期的minimal, reproducible example。显示中间结果与您的预期不同的地方。您发布的代码不会在其他地方运行,包括输入开销,并且不显示问题结构。
  • 请使用这些详细信息更新您的帖子。正如您在尝试评论我的答案时所看到的那样,cmets 不适合代码。从可读部分来看,inc_2 似乎不是eg 中存在的索引;你需要修正这个假设。

标签: python pandas


【解决方案1】:

您使用的语法坚持字符串列表是eg 的合法索引。如果你print(eg),你会发现它没有这样的元素。我认为您的意思是制作一个元素列表,每个元素都由一个字符串索引。

X = [
      eg["p_c_inc_18"],
      eg["ut_hsd_17"],
      eg["d_10"],
      eg["inc_2"]
    ] 

【讨论】:

  • 谢谢。在根据您的 cmets 修改代码后,我重新运行了模型。错误仍然存​​在。
  • 谢谢。我根据您的 cmets 为每个代码重新运行模型。错误仍然存​​在。 KeyError Traceback -> 2646 return self._engine.get_loc(key) 2647 except KeyError: During handling of the above exception, another exception occurred: &lt;ipython-input-1-802920d04782&gt; in &lt;module&gt; 17 eg["d_10"]= eg["Rate_10"]-eg["Rate_06"] 18 inc_2=eg["p_c_inc_18"]*eg["p_c_inc_18"] ---> 19 X = [eg["p_c_inc_18"],eg["ut_hsd_17"],eg["d_10"],eg["inc_2"]]KeyError:'inc_2'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
相关资源
最近更新 更多