Skip to content

Commit d097b0f

Browse files
DOC: python 3 compatibility in code examples (#15011)
1 parent e27b296 commit d097b0f

File tree

5 files changed

+18
-18
lines changed

5 files changed

+18
-18
lines changed

doc/source/categorical.rst

+1-2
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@ To get back to the original Series or `numpy` array, use ``Series.astype(origina
129129
s
130130
s2 = s.astype('category')
131131
s2
132-
s3 = s2.astype('string')
133-
s3
132+
s2.astype(str)
134133
np.asarray(s2)
135134
136135
If you have already `codes` and `categories`, you can use the :func:`~pandas.Categorical.from_codes`

doc/source/cookbook.rst

+5-10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
88
import pandas as pd
99
import numpy as np
10+
from pandas.compat import StringIO
1011
1112
import random
1213
import os
@@ -985,9 +986,6 @@ Skip row between header and data
985986

986987
.. ipython:: python
987988
988-
from io import StringIO
989-
import pandas as pd
990-
991989
data = """;;;;
992990
;;;;
993991
;;;;
@@ -1014,23 +1012,20 @@ Option 1: pass rows explicitly to skiprows
10141012

10151013
.. ipython:: python
10161014
1017-
pd.read_csv(StringIO(data.decode('UTF-8')), sep=';', skiprows=[11,12],
1015+
pd.read_csv(StringIO(data), sep=';', skiprows=[11,12],
10181016
index_col=0, parse_dates=True, header=10)
10191017
10201018
Option 2: read column names and then data
10211019
"""""""""""""""""""""""""""""""""""""""""
10221020

10231021
.. ipython:: python
10241022
1025-
pd.read_csv(StringIO(data.decode('UTF-8')), sep=';',
1026-
header=10, parse_dates=True, nrows=10).columns
1027-
columns = pd.read_csv(StringIO(data.decode('UTF-8')), sep=';',
1028-
header=10, parse_dates=True, nrows=10).columns
1029-
pd.read_csv(StringIO(data.decode('UTF-8')), sep=';',
1023+
pd.read_csv(StringIO(data), sep=';', header=10, nrows=10).columns
1024+
columns = pd.read_csv(StringIO(data), sep=';', header=10, nrows=10).columns
1025+
pd.read_csv(StringIO(data), sep=';', index_col=0,
10301026
header=12, parse_dates=True, names=columns)
10311027
10321028
1033-
10341029
.. _cookbook.sql:
10351030

10361031
SQL

doc/source/gotchas.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ normal Python ``list``. Monotonicity of an index can be tested with the ``is_mon
249249

250250
.. ipython:: python
251251
252-
df = pd.DataFrame(index=[2,3,3,4,5], columns=['data'], data=range(5))
252+
df = pd.DataFrame(index=[2,3,3,4,5], columns=['data'], data=list(range(5)))
253253
df.index.is_monotonic_increasing
254254
255255
# no rows 0 or 1, but still returns rows 2, 3 (both of them), and 4:
@@ -263,7 +263,7 @@ On the other hand, if the index is not monotonic, then both slice bounds must be
263263

264264
.. ipython:: python
265265
266-
df = pd.DataFrame(index=[2,3,1,4,3,5], columns=['data'], data=range(6))
266+
df = pd.DataFrame(index=[2,3,1,4,3,5], columns=['data'], data=list(range(6)))
267267
df.index.is_monotonic_increasing
268268
269269
# OK because 2 and 4 are in the index

doc/source/io.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ worth trying.
502502
.. ipython:: python
503503
:okwarning:
504504
505-
df = pd.DataFrame({'col_1':range(500000) + ['a', 'b'] + range(500000)})
505+
df = pd.DataFrame({'col_1': list(range(500000)) + ['a', 'b'] + list(range(500000))})
506506
df.to_csv('foo')
507507
mixed_df = pd.read_csv('foo')
508508
mixed_df['col_1'].apply(type).value_counts()

doc/source/reshaping.rst

+9-3
Original file line numberDiff line numberDiff line change
@@ -650,10 +650,16 @@ handling of NaN:
650650
because of an ordering bug. See also
651651
`Here <https://github.com/numpy/numpy/issues/641>`__
652652

653-
.. ipython:: python
653+
.. code-block:: ipython
654+
655+
In [2]: pd.factorize(x, sort=True)
656+
Out[2]:
657+
(array([ 2, 2, -1, 3, 0, 1]),
658+
Index([3.14, inf, u'A', u'B'], dtype='object'))
659+
660+
In [3]: np.unique(x, return_inverse=True)[::-1]
661+
Out[3]: (array([3, 3, 0, 4, 1, 2]), array([nan, 3.14, inf, 'A', 'B'], dtype=object))
654662
655-
pd.factorize(x, sort=True)
656-
np.unique(x, return_inverse=True)[::-1]
657663
658664
.. note::
659665
If you just want to handle one column as a categorical variable (like R's factor),

0 commit comments

Comments
 (0)