我正在设置一个Argparse解析器,以便通过shell读取一些用户输入。输入将用于从包含字符串和数字的pandas数据帧中提取数据。我想自动设置Argparse .add_argument()
中的type=
参数,以匹配相应列的数据类型。在
我的想法是设置Argparse参数,其中inputdata是数据帧:
for c in inputdata.columns:
inputname= c
inputtype= np.dtype(inputdata[c])
parser.add_argument("--"+inputname, type=inputtype)
但是,这不起作用:Python引发了一个ValueError: dtype('int64') is not callable
。我想这是因为我没有正确地输入Numpy文件类型;如果我将inputtype设置为float,那么一切都会按照计划进行。如果我手动输入type=np.int64
,Argparse对此也没有问题。在
Common built-in types and functions can be used directly as the value of the type argument
但正如我上面所说的,如果显式地放入numpy数据类型,它似乎没问题。在
谢谢你的帮助!在
使用
或者
^{pr2}$.type
属性是可调用的,可用于创建该数据类型的新实例。在相关问题 更多 >
编程相关推荐