Skip to content

Commit 554c717

Browse files
author
y-p
committed
Merge pull request #6230 from phaebz/PR_py3_doc_compat
DOC/PY3: Python 3 docs example compat for range, StringIO and datetime
2 parents 144bb26 + 8cef96b commit 554c717

10 files changed

+33
-20
lines changed

doc/source/comparison_with_r.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ this:
9999
.. ipython:: python
100100
101101
s = pd.Series(np.arange(5),dtype=np.float32)
102-
Series(pd.match(s,[2,4],np.nan))
102+
pd.Series(pd.match(s,[2,4],np.nan))
103103
104104
For more details and examples see :ref:`the reshaping documentation
105105
<indexing.basics.indexing_isin>`.
@@ -279,7 +279,7 @@ In Python, since ``a`` is a list, you can simply use list comprehension.
279279

280280
.. ipython:: python
281281
282-
a = np.array(range(1,24)+[np.NAN]).reshape(2,3,4)
282+
a = np.array(list(range(1,24))+[np.NAN]).reshape(2,3,4)
283283
DataFrame([tuple(list(x)+[val]) for x, val in np.ndenumerate(a)])
284284
285285
|meltlist|_
@@ -298,7 +298,7 @@ In Python, this list would be a list of tuples, so
298298

299299
.. ipython:: python
300300
301-
a = list(enumerate(range(1,5)+[np.NAN]))
301+
a = list(enumerate(list(range(1,5))+[np.NAN]))
302302
DataFrame(a)
303303
304304
For more details and examples see :ref:`the Into to Data Structures

doc/source/enhancingperf.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ First let's create 4 decent-sized arrays to play with:
384384
from numpy.random import randn
385385
import numpy as np
386386
nrows, ncols = 20000, 100
387-
df1, df2, df3, df4 = [DataFrame(randn(nrows, ncols)) for _ in xrange(4)]
387+
df1, df2, df3, df4 = [DataFrame(randn(nrows, ncols)) for _ in range(4)]
388388
389389
390390
Now let's compare adding them together using plain ol' Python versus

doc/source/io.rst

+8-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
88
import os
99
import csv
10-
from StringIO import StringIO
10+
from pandas.compat import StringIO, BytesIO
1111
import pandas as pd
1212
ExcelWriter = pd.ExcelWriter
1313
@@ -58,6 +58,11 @@ The corresponding ``writer`` functions are object methods that are accessed like
5858
* :ref:`to_clipboard<io.clipboard>`
5959
* :ref:`to_pickle<io.pickle>`
6060

61+
.. note::
62+
For examples that use the ``StringIO`` class, make sure you import it
63+
according to your Python version, i.e. ``from StringIO import StringIO`` for
64+
Python 2 and ``from io import StringIO`` for Python 3.
65+
6166
.. _io.read_csv_table:
6267

6368
CSV & Text files
@@ -278,7 +283,6 @@ used as the column names:
278283

279284
.. ipython:: python
280285
281-
from StringIO import StringIO
282286
data = 'a,b,c\n1,2,3\n4,5,6\n7,8,9'
283287
print(data)
284288
pd.read_csv(StringIO(data))
@@ -327,7 +331,7 @@ result in byte strings being decoded to unicode in the result:
327331
.. ipython:: python
328332
329333
data = b'word,length\nTr\xc3\xa4umen,7\nGr\xc3\xbc\xc3\x9fe,5'.decode('utf8').encode('latin-1')
330-
df = pd.read_csv(StringIO(data), encoding='latin-1')
334+
df = pd.read_csv(BytesIO(data), encoding='latin-1')
331335
df
332336
df['word'][1]
333337
@@ -1561,8 +1565,6 @@ You can even pass in an instance of ``StringIO`` if you so desire
15611565

15621566
.. ipython:: python
15631567
1564-
from cStringIO import StringIO
1565-
15661568
with open(file_path, 'r') as f:
15671569
sio = StringIO(f.read())
15681570
@@ -2627,7 +2629,7 @@ chunks.
26272629
store.append('dfeq', dfeq, data_columns=['number'])
26282630
26292631
def chunks(l, n):
2630-
return [l[i:i+n] for i in xrange(0, len(l), n)]
2632+
return [l[i:i+n] for i in range(0, len(l), n)]
26312633
26322634
evens = [2,4,6,8,10]
26332635
coordinates = store.select_as_coordinates('dfeq','number=evens')

doc/source/remote_data.rst

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
88
import os
99
import csv
10-
from StringIO import StringIO
1110
import pandas as pd
1211
1312
import numpy as np
@@ -49,7 +48,7 @@ Yahoo! Finance
4948
import pandas.io.data as web
5049
import datetime
5150
start = datetime.datetime(2010, 1, 1)
52-
end = datetime.datetime(2013, 01, 27)
51+
end = datetime.datetime(2013, 1, 27)
5352
f=web.DataReader("F", 'yahoo', start, end)
5453
f.ix['2010-01-04']
5554
@@ -63,7 +62,7 @@ Google Finance
6362
import pandas.io.data as web
6463
import datetime
6564
start = datetime.datetime(2010, 1, 1)
66-
end = datetime.datetime(2013, 01, 27)
65+
end = datetime.datetime(2013, 1, 27)
6766
f=web.DataReader("F", 'google', start, end)
6867
f.ix['2010-01-04']
6968
@@ -77,7 +76,7 @@ FRED
7776
import pandas.io.data as web
7877
import datetime
7978
start = datetime.datetime(2010, 1, 1)
80-
end = datetime.datetime(2013, 01, 27)
79+
end = datetime.datetime(2013, 1, 27)
8180
gdp=web.DataReader("GDP", "fred", start, end)
8281
gdp.ix['2013-01-01']
8382

doc/source/reshaping.rst

+5
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,11 @@ To encode 1-d values as an enumerated type use ``factorize``:
436436
Note that ``factorize`` is similar to ``numpy.unique``, but differs in its
437437
handling of NaN:
438438

439+
.. note::
440+
The following ``numpy.unique`` will fail under Python 3 with a ``TypeError``
441+
because of an ordering bug. See also
442+
`Here <https://github.com/numpy/numpy/issues/641>`__
443+
439444
.. ipython:: python
440445
441446
pd.factorize(x, sort=True)

doc/source/v0.10.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
.. ipython:: python
44
:suppress:
55

6-
from StringIO import StringIO
6+
from pandas.compat import StringIO
77

88
v0.10.0 (December 17, 2012)
99
---------------------------

doc/source/v0.13.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ Experimental
698698

699699
nrows, ncols = 20000, 100
700700
df1, df2, df3, df4 = [DataFrame(randn(nrows, ncols))
701-
for _ in xrange(4)]
701+
for _ in range(4)]
702702

703703
.. ipython:: python
704704

doc/source/v0.13.1.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ API changes
141141
.. ipython:: python
142142

143143
def applied_func(col):
144-
print "Apply function being called with:", col
144+
print("Apply function being called with: ", col)
145145
return col.sum()
146146

147147
empty = DataFrame(columns=['a', 'b'])

doc/source/v0.9.0.txt

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
.. _whatsnew_0900:
22

3+
.. ipython:: python
4+
:suppress:
5+
6+
from pandas.compat import StringIO
7+
38
v0.9.0 (October 7, 2012)
49
------------------------
510

@@ -36,8 +41,6 @@ API changes
3641

3742
.. ipython:: python
3843

39-
from StringIO import StringIO
40-
4144
data = '0,0,1\n1,1,0\n0,1,0'
4245
df = read_csv(StringIO(data), header=None)
4346
df

doc/source/v0.9.1.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
.. _whatsnew_0901:
22

3+
.. ipython:: python
4+
:suppress:
5+
6+
from pandas.compat import StringIO
7+
38
v0.9.1 (November 14, 2012)
49
--------------------------
510

@@ -132,7 +137,6 @@ API changes
132137

133138
data = 'A,B,C\n00001,001,5\n00002,002,6'
134139

135-
from cStringIO import StringIO
136140
read_csv(StringIO(data), converters={'A' : lambda x: x.strip()})
137141

138142

0 commit comments

Comments
 (0)