我正在设置一个Argparse解析器来通过shell读取一些用户输入.输入将用于从包含字符串和数字的pandas DataFrame中提取数据.我想在Argparse.add_argument()中自动设置type =参数以匹配相应列的数据类型.
我的想法是设置像这样的Argparse参数,其中inputdata是DataFrame:
for c in inputdata.columns:
inputname= c
inputtype= np.dtype(inputdata[c])
parser.add_argument("--"+inputname, type=inputtype)
但是,这不起作用:Python引发了一个ValueError:dtype(‘int64’)不可调用.我想这是因为我没有给它Numpy文件类型喂它;如果我,例如将inputtype设置为float,一切按计划进行.如果我手动输入type = np.int64,Argparse也没有问题.
>我怎样才能让它接受我的DataFrame中的文件类型,即上面显示的循环中的int64和object?我尝试了一些选项here,例如dtype.type但没有任何效果.
>或者这是不可能的? Argparse docs只表明了这一点
Common built-in types and functions can be used directly as the value of the type argument
但正如我上面所说,如果明确放入,numpy数据类型似乎很好.
谢谢你的帮助!
解决方法:
使用
inputtype = np.dtype(inputdata[c]).type
要么
inputtype = inputdata[c].dtype.type
.type属性是可调用的,可用于创建该dtype的新实例.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。