8000 DOC: fix broken genfromtxt examples in user guide. Closes gh-7662. · numpy/numpy@e928bd9 · GitHub
[go: up one dir, main page]

Skip to content

Commit e928bd9

Browse files
committed
DOC: fix broken genfromtxt examples in user guide. Closes gh-7662.
[ci skip]
1 parent e6593fb commit e928bd9

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

doc/source/user/basics.io.genfromtxt.rst

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ other faster and simpler functions like :func:`~numpy.loadtxt` cannot.
1919
When giving examples, we will use the following conventions::
2020

2121
>>> import numpy as np
22-
>>> from StringIO import StringIO
22+
>>> from io import BytesIO
2323

2424

2525

@@ -59,7 +59,7 @@ example, comma-separated files (CSV) use a comma (``,``) or a semicolon
5959
(``;``) as delimiter::
6060

6161
>>> data = "1, 2, 3\n4, 5, 6"
62-
>>> np.genfromtxt(StringIO(data), delimiter=",")
62+
>>> np.genfromtxt(BytesIO(data), delimiter=",")
6363
array([[ 1., 2., 3.],
6464
[ 4., 5., 6.]])
6565

@@ -75,12 +75,12 @@ defined as a given number of characters. In that case, we need to set
7575
size) or to a sequence of integers (if columns can have different sizes)::
7676

7777
>>> data = " 1 2 3\n 4 5 67\n890123 4"
78-
>>> np.genfromtxt(StringIO(data), delimiter=3)
78+
>>> np.genfromtxt(BytesIO(data), delimiter=3)
7979
array([[ 1., 2., 3.],
8080
[ 4., 5., 67.],
8181
[ 890., 123., 4.]])
8282
>>> data = "123456789\n 4 7 9\n 4567 9"
83-
>>> np.genfromtxt(StringIO(data), delimiter=(4, 3, 2))
83+
>>> np.genfromtxt(BytesIO(data), delimiter=(4, 3, 2))
8484
array([[ 1234., 567., 89.],
8585
[ 4., 7., 9.],
8686
[ 4., 567., 9.]])
@@ -96,12 +96,12 @@ This behavior can be overwritten by setting the optional argument
9696

9797
>>> data = "1, abc , 2\n 3, xxx, 4"
9898
>>> # Without autostrip
99-
>>> np.genfromtxt(StringIO(data), delimiter=",", dtype="|S5")
99+
>>> np.genfromtxt(BytesIO(data), delimiter=",", dtype="|S5")
100100
array([['1', ' abc ', ' 2'],
101101
['3', ' xxx', ' 4']],
102102
dtype='|S5')
103103
>>> # With autostrip
104-
>>> np.genfromtxt(StringIO(data), delimiter=",", dtype="|S5", autostrip=True)
104+
>>> np.genfromtxt(BytesIO(data), delimiter=",", dtype="|S5", autostrip=True)
105105
array([['1', 'abc', '2'],
106106
['3', 'xxx', '4']],
107107
dtype='|S5')
@@ -126,7 +126,7 @@ marker(s) is simply ignored::
126126
... # And here comes the last line
127127
... 9, 0
128128
... """
129-
>>> np.genfromtxt(StringIO(data), comments="#", delimiter=",")
129+
>>> np.genfromtxt(BytesIO(data), comments="#", delimiter=",")
130130
[[ 1. 2.]
131131
[ 3. 4.]
132132
[ 5. 6.]
@@ -154,9 +154,9 @@ performed. Similarly, we can skip the last ``n`` lines of the file by
154154
using the :keyword:`skip_footer` attribute and giving it a value of ``n``::
155155

156156
>>> data = "\n".join(str(i) for i in range(10))
157-
>>> np.genfromtxt(StringIO(data),)
157+
>>> np.genfromtxt(BytesIO(data),)
158158
array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
159-
>>> np.genfromtxt(StringIO(data),
159+
>>> np.genfromtxt(BytesIO(data),
160160
... skip_header=3, skip_footer=5)
161161
array([ 3., 4.])
162162

@@ -178,7 +178,7 @@ For example, if we want to import only the first and the last columns, we
178178
can use ``usecols=(0, -1)``::
179179

180180
>>> data = "1 2 3\n4 5 6"
181-
>>> np.genfromtxt(StringIO(data), usecols=(0, -1))
181+
>>> np.genfromtxt(BytesIO(data), usecols=(0, -1))
182182
array([[ 1., 3.],
183183
[ 4., 6.]])
184184

@@ -187,11 +187,11 @@ giving their name to the :keyword:`usecols` argument, either as a sequence
187187
of strings or a comma-separated string::
188188

189189
>>> data = "1 2 3\n4 5 6"
190-
>>> np.genfromtxt(StringIO(data),
190+
>>> np.genfromtxt(BytesIO(data),
191191
... names="a, b, c", usecols=("a", "c"))
192192
array([(1.0, 3.0), (4.0, 6.0)],
193193
dtype=[('a', '<f8'), ('c', '<f8')])
194-
>>> np.genfromtxt(StringIO(data),
194+
>>> np.genfromtxt(BytesIO(data),
195195
... names="a, b, c", usecols=("a, c"))
196196
array([(1.0, 3.0), (4.0, 6.0)],
197197
dtype=[('a', '<f8'), ('c', '<f8')])
@@ -249,15 +249,15 @@ A natural approach when dealing with tabular data is to allocate a name to
249249
each column. A first possibility is to use an explicit structured dtype,
250250
as mentioned previously::
251251

252-
>>> data = StringIO("1 2 3\n 4 5 6")
252+
>>> data = BytesIO("1 2 3\n 4 5 6")
253253
>>> np.genfr 6D47 omtxt(data, dtype=[(_, int) for _ in "abc"])
254254
array([(1, 2, 3), (4, 5, 6)],
255255
dtype=[('a', '<i8'), ('b', '<i8'), ('c', '<i8')])
256256

257257
Another simpler possibility is to use the :keyword:`names` keyword with a
258258
sequence of strings or a comma-separated string::
259259

260-
>>> data = StringIO("1 2 3\n 4 5 6")
260+
>>> data = BytesIO("1 2 3\n 4 5 6")
261261
>>> np.genfromtxt(data, names="A, B, C")
262262
array([(1.0, 2.0, 3.0), (4.0, 5.0, 6.0)],
263263
dtype=[('A', '<f8'), ('B', '<f8'), ('C', '<f8')])
@@ -271,7 +271,7 @@ that case, we must use the :keyword:`names` keyword with a value of
271271
``True``. The names will then be read from the first line (after the
272272
``skip_header`` ones), even if the line is commented out::
273273

274-
>>> data = StringIO("So it goes\n#a b c\n1 2 3\n 4 5 6")
274+
>>> data = BytesIO("So it goes\n#a b c\n1 2 3\n 4 5 6")
275275
>>> np.genfromtxt(data, skip_header=1, names=True)
276276
array([(1.0, 2.0, 3.0), (4.0, 5.0, 6.0)],
277277
dtype=[('a', '<f8'), ('b', '<f8'), ('c', '<f8')])
@@ -280,7 +280,7 @@ The default value of :keyword:`names` is ``None``. If we give any other
280280
value to the keyword, the new names will overwrite the field names we may
281281
have defined with the dtype::
282282

283-
>>> data = StringIO("1 2 3\n 4 5 6")
283+
>>> data = BytesIO("1 2 3\n 4 5 6")
284284
>>> ndtype=[('a',int), ('b', float), ('c', int)]
285285
>>> names = ["A", "B", "C"]
286286
>>> np.genfromtxt(data, names=names, dtype=ndtype)
@@ -295,23 +295,23 @@ If ``names=None`` but a structured dtype is expected, names are defined
295295
with the standard NumPy default of ``"f%i"``, yielding names like ``f0``,
296296
``f1`` and so forth::
297297

298-
>>> data = StringIO("1 2 3\n 4 5 6")
298+
>>> data = BytesIO("1 2 3\n 4 5 6")
299299
>>> np.genfromtxt(data, dtype=(int, float, int))
300300
array([(1, 2.0, 3), (4, 5.0, 6)],
301301
dtype=[('f0', '<i8'), ('f1', '<f8'), ('f2', '<i8')])
302302

303303
In the same way, if we don't give enough names to match the length of the
304304
dtype, the missing names will be defined with this default template::
305305

306-
>>> data = StringIO("1 2 3\n 4 5 6")
306+
>>> data = BytesIO("1 2 3\n 4 5 6")
307307
>>> np.genfromtxt(data, dtype=(int, float, int), names="a")
308308
array([(1, 2.0, 3), (4, 5.0, 6)],
309309
dtype=[('a', '<i8'), ('f0', '<f8'), ('f1', '<i8')])
310310

311311
We can overwrite this default with the :keyword:`defaultfmt` argument, that
312312
takes any format string::
313313

314-
>>> data = StringIO("1 2 3\n 4 5 6")
314+
>>> data = BytesIO("1 2 3\n 4 5 6")
315315
>>> np.genfromtxt(data, dtype=(int, float, int), defaultfmt="var_%02i")
316316
array([(1, 2.0, 3), (4, 5.0, 6)],
317317
dtype=[('var_00', '<i8'), ('var_01', '<f8'), ('var_02', '<i8')])
@@ -377,7 +377,7 @@ representing a percentage to a float between 0 and 1::
377377
>>> data = "1, 2.3%, 45.\n6, 78.9%, 0"
378378
>>> names = ("i", "p", "n")
379379
>>> # General case .....
380-
>>> np.genfromtxt(StringIO(data), delimiter=",", names=names)
380+
>>> np.genfromtxt(BytesIO(data), delimiter=",", names=names)
381381
array([(1.0, nan, 45.0), (6.0, nan, 0.0)],
382382
dtype=[('i', '<f8'), ('p', '<f8'), ('n', '<f8')])
383383

@@ -387,7 +387,7 @@ and ``' 78.9%'`` cannot be converted to float and we end up having
387387
``np.nan`` instead. Let's now use a converter::
388388

389389
>>> # Converted case ...
390-
>>> np.genfromtxt(StringIO(data), delimiter=",", names=names,
390+
>>> np.genfromtxt(BytesIO(data), delimiter=",", names=names,
391391
... converters={1: convertfunc})
392392
array([(1.0, 0.023, 45.0), (6.0, 0.78900000000000003, 0.0)],
393393
dtype=[('i', '<f8'), ('p', '<f8'), ('n', '<f8')])
@@ -396,7 +396,7 @@ The same results can be obtained by using the name of the second column
396396
(``"p"``) as key instead of its index (1)::
397397

398398
>>> # Using a name for the converter ...
399-
>>> np.genfromtxt(StringIO(data), delimiter=",", names=names,
399+
>>> np.genfromtxt(BytesIO(data), delimiter=",", names=names,
400400
... converters={"p": convertfunc})
401401
array([(1.0, 0.023, 45.0), (6.0, 0.78900000000000003, 0.0)],
402402
dtype=[('i', '<f8'), ('p', '<f8'), ('n', '<f8')])
@@ -410,8 +410,8 @@ by default::
410410

411411
>>> data = "1, , 3\n 4, 5, 6"
412412
>>> convert = lambda x: float(x.strip() or -999)
413-
>>> np.genfromtxt(StringIO(data), delimiter=",",
414-
... converter={1: convert})
413+
>>> np.genfromtxt(BytesIO(data), delimiter=",",
414+
... converters={1: convert})
415415
array([[ 1., -999., 3.],
416416
[ 4., 5., 6.]])
417417

@@ -492,7 +492,7 @@ and second column, and to -999 if they occur in the last column::
492492
... names="a,b,c",
493493
... missing_values={0:"N/A", 'b':" ", 2:"???"},
494494
... filling_values={0:0, 'b':0, 2:-999})
495-
>>> np.genfromtxt(StringIO.StringIO(data), **kwargs)
495+
>>> np.genfromtxt(BytesIO(data), **kwargs)
496496
array([(0, 2, 3), (4, 0, -999)],
497497
dtype=[('a', '<i8'), ('b', '<i8'), ('c', '<i8')])
498498

0 commit comments

Comments
 (0)
0