I am setting up a Argparse parser to read some user input through the shell. The input will be used to extract data from a pandas DataFrame containing strings and numbers. I want to automatically set the type=
argument in Argparse.add_argument()
to match the datatype of the respective column.
My idea was to set up the Argparse arguments like this, where inputdata is the DataFrame:
for c in inputdata.columns:
inputname= c
inputtype= np.dtype(inputdata[c])
parser.add_argument("--"+inputname, type=inputtype)
However, this does not work: Python raises a ValueError: dtype('int64') is not callable
. I figure this is because I'm not feeding it the Numpy filetypes right; if I e.g. set inputtype to float, all goes according to plan. If I manually enter type=np.int64
, Argparse has no problem with this either.
How can I get it to accept the filetypes in my DataFrame, namely int64 and object in the loop shown above? I tried some of the options here as well, e.g. dtype.type
but nothing worked.
Or is this just not possible? The Argparse docs state only that
Common built-in types and functions can be used directly as the value of the type argument
but as I said above it seems fine with numpy datatype if put in explicitly.
Thanks for your help!
Use
inputtype = np.dtype(inputdata[c]).type
or
inputtype = inputdata[c].dtype.type
The .type
attribute is callable, and can be used to create new instances of that dtype.