在argpars中使用numpy数据类型

2024-04-20 09:43:08 发布

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

我正在设置一个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对此也没有问题。在

  • 如何让它接受数据帧中的文件类型,即上面所示循环中的int64object?我也尝试了一些选项here,例如dtype.type,但没有任何效果。

  • 或者这是不可能的吗?Argparsedocs只声明

Common built-in types and functions can be used directly as the value of the type argument

但正如我上面所说的,如果显式地放入numpy数据类型,它似乎没问题。在

谢谢你的帮助!在


Tags: 数据inadd参数typenpargparseargument