从 Stata 17 调用 Python 时出现问题

发布于 2025-01-11 05:34:32 字数 800 浏览 0 评论 0原文

下面的第一个代码在 python 中运行没有问题,但是当在 Stata do 文件中使用相同的行时,会出现一条消息,提示 Python 编码错误。知道什么可能导致这个问题吗? (我在 Windows 上使用 Stata 17。)

# PYTHON CODE: 
import pandas as pd
df = pd.read_stata('data.dta')  
cut_bins = [-5, -4, -3, -2, -1, 0, 1, 2, 3]
df['cut_bins'] = pd.cut(df['v'], bins=cut_bins)
* STATA CODE: 
python 
import pandas as pd
from sfi import Data
cut_bins = [-5, -4, -3, -2, -1, 0, 1, 2, 3]
df = Data.get('id bin v')
df["cut_bins"] = pd.cut(df['v'], bins=cut_bins)
end 

在 do 文件中运行 Stata 代码时,会出现以下消息:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers or slices, not str

This suggest a Python编码错误,但由于相同的行在Python中运行没有错误,因此也可能存在是Stata的问题。

The first code below runs without problems in python, but when using the same lines in a Stata do-file, a message appears suggesting a Python coding error. Any idea what could cause this problem? (I am using Stata 17 on Windows.)

# PYTHON CODE: 
import pandas as pd
df = pd.read_stata('data.dta')  
cut_bins = [-5, -4, -3, -2, -1, 0, 1, 2, 3]
df['cut_bins'] = pd.cut(df['v'], bins=cut_bins)
* STATA CODE: 
python 
import pandas as pd
from sfi import Data
cut_bins = [-5, -4, -3, -2, -1, 0, 1, 2, 3]
df = Data.get('id bin v')
df["cut_bins"] = pd.cut(df['v'], bins=cut_bins)
end 

When running the Stata code in a do-file, the following message appears:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers or slices, not str

This suggests a Python coding error but since the identical lines run in Python without error, it may as well be that there is a problem with Stata.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情场扛把子 2025-01-18 05:34:32

df = Data.get('id bin v') 将返回变量 idbinv在列表的列表中。即df[0]保存idbinv的第一行值。

现在尝试一下

* STATA CODE: 
python 
import pandas as pd
from sfi import Data
cut_bins = range(-5,4)
df = pd.DataFrame(Data.getAsDict('id bin v'))
df["cut_bins"] = pd.cut(df['v'], bins=cut_bins)
end

df.head() 将生成(使用我的 idbinv< 的假数据/代码>):

    id  bin         v  cut_bins
0  1.0  1.0  2.535117    (2, 3]
1  2.0  2.0  0.161955    (0, 1]
2  3.0  3.0 -0.397220   (-1, 0]
3  4.0  4.0  0.003145    (0, 1]
4  5.0  5.0 -2.985772  (-3, -2]

df = Data.get('id bin v') will return the variables id, bin, and v in a list of lists. ie df[0] hold the first row of values of id, bin, and v.

Try this

* STATA CODE: 
python 
import pandas as pd
from sfi import Data
cut_bins = range(-5,4)
df = pd.DataFrame(Data.getAsDict('id bin v'))
df["cut_bins"] = pd.cut(df['v'], bins=cut_bins)
end

Now, df.head() would produce (using my fake data for id,bin and v):

    id  bin         v  cut_bins
0  1.0  1.0  2.535117    (2, 3]
1  2.0  2.0  0.161955    (0, 1]
2  3.0  3.0 -0.397220   (-1, 0]
3  4.0  4.0  0.003145    (0, 1]
4  5.0  5.0 -2.985772  (-3, -2]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文