Skip to content

DOC: python 3 compatibility in code examples #15011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions doc/source/categorical.rst
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ To get back to the original Series or `numpy` array, use ``Series.astype(origina
s
s2 = s.astype('category')
s2
s3 = s2.astype('string')
s3
s2.astype(str)
np.asarray(s2)

If you have already `codes` and `categories`, you can use the :func:`~pandas.Categorical.from_codes`
Expand Down
15 changes: 5 additions & 10 deletions doc/source/cookbook.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import pandas as pd
import numpy as np
from pandas.compat import StringIO

import random
import os
Expand Down Expand Up @@ -985,9 +986,6 @@ Skip row between header and data

.. ipython:: python

from io import StringIO
import pandas as pd

data = """;;;;
;;;;
;;;;
Expand All @@ -1014,23 +1012,20 @@ Option 1: pass rows explicitly to skiprows

.. ipython:: python

pd.read_csv(StringIO(data.decode('UTF-8')), sep=';', skiprows=[11,12],
pd.read_csv(StringIO(data), sep=';', skiprows=[11,12],
index_col=0, parse_dates=True, header=10)

Option 2: read column names and then data
"""""""""""""""""""""""""""""""""""""""""

.. ipython:: python

pd.read_csv(StringIO(data.decode('UTF-8')), sep=';',
header=10, parse_dates=True, nrows=10).columns
columns = pd.read_csv(StringIO(data.decode('UTF-8')), sep=';',
header=10, parse_dates=True, nrows=10).columns
pd.read_csv(StringIO(data.decode('UTF-8')), sep=';',
pd.read_csv(StringIO(data), sep=';', header=10, nrows=10).columns
columns = pd.read_csv(StringIO(data), sep=';', header=10, nrows=10).columns
pd.read_csv(StringIO(data), sep=';', index_col=0,
header=12, parse_dates=True, names=columns)



.. _cookbook.sql:

SQL
Expand Down
4 changes: 2 additions & 2 deletions doc/source/gotchas.rst
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ normal Python ``list``. Monotonicity of an index can be tested with the ``is_mon

.. ipython:: python

df = pd.DataFrame(index=[2,3,3,4,5], columns=['data'], data=range(5))
df = pd.DataFrame(index=[2,3,3,4,5], columns=['data'], data=list(range(5)))
df.index.is_monotonic_increasing

# no rows 0 or 1, but still returns rows 2, 3 (both of them), and 4:
Expand All @@ -263,7 +263,7 @@ On the other hand, if the index is not monotonic, then both slice bounds must be

.. ipython:: python

df = pd.DataFrame(index=[2,3,1,4,3,5], columns=['data'], data=range(6))
df = pd.DataFrame(index=[2,3,1,4,3,5], columns=['data'], data=list(range(6)))
df.index.is_monotonic_increasing

# OK because 2 and 4 are in the index
Expand Down
2 changes: 1 addition & 1 deletion doc/source/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ worth trying.
.. ipython:: python
:okwarning:

df = pd.DataFrame({'col_1':range(500000) + ['a', 'b'] + range(500000)})
df = pd.DataFrame({'col_1': list(range(500000)) + ['a', 'b'] + list(range(500000))})
df.to_csv('foo')
mixed_df = pd.read_csv('foo')
mixed_df['col_1'].apply(type).value_counts()
Expand Down
12 changes: 9 additions & 3 deletions doc/source/reshaping.rst
Original file line number Diff line number Diff line change
Expand Up @@ -650,10 +650,16 @@ handling of NaN:
because of an ordering bug. See also
`Here <https://github.com/numpy/numpy/issues/641>`__

.. ipython:: python
.. code-block:: ipython

In [2]: pd.factorize(x, sort=True)
Out[2]:
(array([ 2, 2, -1, 3, 0, 1]),
Index([3.14, inf, u'A', u'B'], dtype='object'))

In [3]: np.unique(x, return_inverse=True)[::-1]
Out[3]: (array([3, 3, 0, 4, 1, 2]), array([nan, 3.14, inf, 'A', 'B'], dtype=object))

pd.factorize(x, sort=True)
np.unique(x, return_inverse=True)[::-1]

.. note::
If you just want to handle one column as a categorical variable (like R's factor),
Expand Down