时间维度上的索引必须是数字或类似日期的错误

2024-04-28 21:20:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试着做一个面板回归

from linearmodels.panel import PooledOLS
import statsmodels.api as sm

但是我的索引遇到了一个问题。对于回归,我需要一个多重索引,所以我有一个虚拟变量和时间(见下文)。这两个指标是a_c(我分析的所有10个国家的虚拟变量)和时间戳(日期)

                i_ct    preemu  postemu  γ1             γ2
α_c Timestamp   

10  2020-06-24  -0.04   0.00    0.02    -1.110223e-16   2.000000e-02
    2020-06-25  0.05    0.00    -0.04   -1.000000e-02   1.000000e-02
    2020-06-26  0.02    0.00    0.05    0.000000e+00    1.000000e-02
    2020-06-29  0.00    0.00    0.02    -2.000000e-02   -1.110223e-16
    2020-06-30  0.08    0.00    0.00    2.000000e-02    -6.000000e-02   

当我运行回归时

exog_vars = ['preemu','postemu','γ1','γ2']
exog = sm.add_constant(beta_panel[exog_vars])
mod = PooledOLS(beta_panel.i_ct, exog)
pooled_res = mod.fit()
print(pooled_res)

我得到这个错误:

ValueError: The index on the time dimension must be either numeric or date-like

但当时间戳不在索引中时,我会得到以下错误:

Series can only be used with a 2-level MultiIndex

有人知道为什么它会抛出第一个错误吗


Tags: importmod错误时间resvarsbetasm
2条回答

提供的示例中的时间维度索引看起来像一个字符串。将其转换为日期格式或数字格式应该可以

还要注意,实体索引按顺序排在第一位,时间维度在多索引中排在第二位,正如所提供的示例所正确完成的那样

可能导致错误的小疏忽

将字符串date-time类型更改为numerical(我试图将其更改为timestamp或其他时间数据类型,但错误仍然存在),例如:2021/01/31更改为20210131。然后它就起作用了

相关问题 更多 >