User guide

NumPy User Guide, Release 1.9.0
>>> data = "N/A, 2, 3\n4, ,???"
>>> kwargs = dict(delimiter=",",
... dtype=int,
... names="a,b,c",
... missing_values={0:"N/A", ’b’:" ", 2:"???"},
... filling_values={0:0, ’b’:0, 2:-999})
>>> np.genfromtxt(StringIO.StringIO(data),
**
kwargs)
array([(0, 2, 3), (4, 0, -999)],
dtype=[(’a’, ’<i8’), (’b’, ’<i8’), (’c’, ’<i8’)])
usemask
We may also want to keep track of the occurrence of missing data by constructing a boolean mask, with True entries
where data was missing and False otherwise. To do that, we just have to set the optional argument usemask to
True (the default is False). The output array will then be a MaskedArray.
Shortcut functions
In addition to genfromtxt, the numpy.lib.io module provides several convenience functions derived from
genfromtxt. These functions work the same way as the original, but they have different default values.
ndfromtxt
Always set usemask=False. The output is always a standard numpy.ndarray.
mafromtxt
Always set usemask=True. The output is always a MaskedArray
recfromtxt
Returns a standard numpy.recarray (if usemask=False) or a MaskedRecords array (if
usemaske=True). The default dtype is dtype=None, meaning that the types of each column will be auto-
matically determined.
recfromcsv
Like recfromtxt, but with a default delimiter=",".
2.4 Indexing
See Also:
Indexing routines
Array indexing refers to any use of the square brackets ([]) to index array values. There are many options to indexing,
which give numpy indexing great power, but with power comes some complexity and the potential for confusion. This
section is just an overview of the various options and issues related to indexing. Aside from single element indexing,
the details on most of these options are to be found in related sections.
2.4.1 Assignment vs referencing
Most of the following examples show the use of indexing when referencing data in an array. The examples work just as
well when assigning to an array. See the section at the end for specific examples and explanations on how assignments
work.
20 Chapter 2. Numpy basics