summaryrefslogtreecommitdiff
path: root/numpy/lib/npyio.py
diff options
context:
space:
mode:
authorRoss Barnowski <rossbar@berkeley.edu>2022-01-11 17:26:50 -0800
committerSebastian Berg <sebastian@sipsolutions.net>2022-01-18 14:01:01 -0600
commitecff02c30d774e3588535d85f9152ea6066d0b24 (patch)
tree22c3428b5680a43b34d915405f737a005d8bc701 /numpy/lib/npyio.py
parent3e0d4329e27eca7c63fd894020091bfae5091560 (diff)
downloadnumpy-ecff02c30d774e3588535d85f9152ea6066d0b24.tar.gz
Update and add converters examples.
Diffstat (limited to 'numpy/lib/npyio.py')
-rw-r--r--numpy/lib/npyio.py34
1 files changed, 33 insertions, 1 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py
index d22d83506..87fa2e4e2 100644
--- a/numpy/lib/npyio.py
+++ b/numpy/lib/npyio.py
@@ -1204,6 +1204,29 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None,
>>> y
array([2., 4.])
+ The `converters` argument is used to specify functions to preprocess the
+ text prior to parsing. `converters` can be a dictionary that maps
+ preprocessing functions to each column:
+
+ >>> s = StringIO("1.618, 2.296\n3.141, 4.669\n")
+ >>> conv = {
+ ... 0: lambda x: np.floor(float(x)), # conversion fn for column 0
+ ... 1: lambda x: np.ceil(float(x)), # conversion fn for column 1
+ ... }
+ >>> np.loadtxt(s, delimiter=",", converters=conv)
+ array([[1., 3.],
+ [3., 5.]])
+
+ `converters` can be a callable instead of a dictionary, in which case it
+ is applied to all columns:
+
+ >>> s = StringIO("0xDE 0xAD\n0xC0 0xDE")
+ >>> import functools
+ >>> conv = functools.partial(int, base=16)
+ >>> np.loadtxt(s, converters=conv)
+ array([[222., 173.],
+ [192., 222.]])
+
This example shows how `converters` can be used to convert a field
with a trailing minus sign into a negative number.
@@ -1211,10 +1234,19 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None,
>>> def conv(fld):
... return -float(fld[:-1]) if fld.endswith(b'-') else float(fld)
...
- >>> np.loadtxt(s, converters={0: conv, 1: conv})
+ >>> np.loadtxt(s, converters=conv)
array([[ 10.01, -31.25],
[ 19.22, 64.31],
[-17.57, 63.94]])
+
+ Note that with the default ``encoding="bytes"``, the inputs to the
+ converter function are latin-1 encoded byte strings. To deactivate the
+ implicit encoding prior to conversion, behavior use ``encoding=None``
+
+ >>> s = StringIO('10.01 31.25-\n19.22 64.31\n17.57- 63.94')
+ >>> conv = lambda x: -float(x[:-1]) if x.endswith('-') else float(x)
+ >>> np.loadtxt(s, converters=conv, encoding=None)
+
"""
if like is not None: