Importing data with genfromtxt
NumPy provides several functions to create arrays from tabular data.
We focus here on the genfromtxt function.
In a nutshell, genfromtxt runs two main loops. The first
loop converts each line of the file in a sequence of strings. The second
loop converts each string to the appropriate data type. This mechanism is
slower than a single loop, but gives more flexibility. In particular,
genfromtxt is able to take missing data into account, when
other faster and simpler functions like loadtxt cannot.
Defining the input
The only mandatory argument of genfromtxt is the source of
the data. It can be a string, a list of strings, a generator or an open
file-like object with a read method, for example, a file or
io.StringIO object. If a single string is provided, it is assumed
to be the name of a local or remote file. If a list of strings or a generator
returning strings is provided, each string is treated as one line in a file.
When the URL of a remote file is passed, the file is automatically downloaded
to the current directory and opened.
Recognized file types are text files and archives. Currently, the function
recognizes gzip and bz2 (bzip2) archives. The type of
the archive is determined from the extension of the file: if the filename
ends with '.gz', a archive is expected; if it ends with
, a archive is assumed.
Splitting the lines into columns
The delimiter argument
Once the file is defined and open for reading, genfromtxt
splits each non-empty line into a sequence of strings. Empty or commented
lines are just skipped. The delimiter keyword is used to define
how the splitting should take place.
Quite often, a single character marks the separation between columns. For
example, comma-separated files (CSV) use a comma (,) or a semicolon
(;) as delimiter:
>>> data = "1, 2, 3\n4, 5, 6"
Skipping lines and choosing columns
Choosing the data type
The main way to control how the sequences of strings we have read from the
file are converted to other types is to set the dtype argument.
Acceptable values for this argument are:
a single type, such as
dtype=np.float64. The output will be 2D with the given dtype, unless a name has been associated with each column with the use of thenamesargument (see below). Note thatdtype=np.float64is the default forgenfromtxt.a sequence of types, such as
dtype=(np.int_, np.float64, np.float64).a comma-separated string, such as
dtype="i4,f8,|U3".a dictionary with two keys
and .
Setting the names
The names argument
A natural approach when dealing with tabular data is to allocate a name to each column. A first possibility is to use an explicit structured dtype, as mentioned previously:
>>> data = StringIO("1 2 3\n 4 5 6")
>>> np.genfromtxt(data, dtype=[(_, np.int_)
Tweaking the conversion
The converters argument
Usually, defining a dtype is sufficient to define how the sequence of
strings must be converted. However, some additional control may sometimes
be required. For example, we may want to make sure that a date in a format
YYYY/MM/DD is converted to a datetime object, or that
a string like xx% is properly converted to a float between 0 and 1. In
such cases, we should define conversion functions with the converters
arguments.
The value of this argument is typically a dictionary with column indices or column names as keys and a conversion functions as values. These conversion functions can either be actual functions or lambda functions. In any case, they should accept only a string as input and output only a single element of the wanted type.
In the following example, the second column is converted from as string representing a percentage to a float between 0 and 1: