Skip to content

DOC/PY3: Python 3 docs example compat for range, StringIO and datetime #6230

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
2 commits merged into from
Feb 4, 2014
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
6 changes: 3 additions & 3 deletions doc/source/comparison_with_r.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ this:
.. ipython:: python

s = pd.Series(np.arange(5),dtype=np.float32)
Series(pd.match(s,[2,4],np.nan))
pd.Series(pd.match(s,[2,4],np.nan))

For more details and examples see :ref:`the reshaping documentation
<indexing.basics.indexing_isin>`.
Expand Down Expand Up @@ -279,7 +279,7 @@ In Python, since ``a`` is a list, you can simply use list comprehension.

.. ipython:: python

a = np.array(range(1,24)+[np.NAN]).reshape(2,3,4)
a = np.array(list(range(1,24))+[np.NAN]).reshape(2,3,4)
DataFrame([tuple(list(x)+[val]) for x, val in np.ndenumerate(a)])

|meltlist|_
Expand All @@ -298,7 +298,7 @@ In Python, this list would be a list of tuples, so

.. ipython:: python

a = list(enumerate(range(1,5)+[np.NAN]))
a = list(enumerate(list(range(1,5))+[np.NAN]))
DataFrame(a)

For more details and examples see :ref:`the Into to Data Structures
Expand Down
2 changes: 1 addition & 1 deletion doc/source/enhancingperf.rst
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ First let's create 4 decent-sized arrays to play with:
from numpy.random import randn
import numpy as np
nrows, ncols = 20000, 100
df1, df2, df3, df4 = [DataFrame(randn(nrows, ncols)) for _ in xrange(4)]
df1, df2, df3, df4 = [DataFrame(randn(nrows, ncols)) for _ in range(4)]


Now let's compare adding them together using plain ol' Python versus
Expand Down
14 changes: 8 additions & 6 deletions doc/source/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import os
import csv
from StringIO import StringIO
from pandas.compat import StringIO, BytesIO
import pandas as pd
ExcelWriter = pd.ExcelWriter

Expand Down Expand Up @@ -58,6 +58,11 @@ The corresponding ``writer`` functions are object methods that are accessed like
* :ref:`to_clipboard<io.clipboard>`
* :ref:`to_pickle<io.pickle>`

.. note::
For examples that use the ``StringIO`` class, make sure you import it
according to your Python version, i.e. ``from StringIO import StringIO`` for
Python 2 and ``from io import StringIO`` for Python 3.

.. _io.read_csv_table:

CSV & Text files
Expand Down Expand Up @@ -278,7 +283,6 @@ used as the column names:

.. ipython:: python

from StringIO import StringIO
data = 'a,b,c\n1,2,3\n4,5,6\n7,8,9'
print(data)
pd.read_csv(StringIO(data))
Expand Down Expand Up @@ -327,7 +331,7 @@ result in byte strings being decoded to unicode in the result:
.. ipython:: python

data = b'word,length\nTr\xc3\xa4umen,7\nGr\xc3\xbc\xc3\x9fe,5'.decode('utf8').encode('latin-1')
df = pd.read_csv(StringIO(data), encoding='latin-1')
df = pd.read_csv(BytesIO(data), encoding='latin-1')
df
df['word'][1]

Expand Down Expand Up @@ -1561,8 +1565,6 @@ You can even pass in an instance of ``StringIO`` if you so desire

.. ipython:: python

from cStringIO import StringIO

with open(file_path, 'r') as f:
sio = StringIO(f.read())

Expand Down Expand Up @@ -2627,7 +2629,7 @@ chunks.
store.append('dfeq', dfeq, data_columns=['number'])

def chunks(l, n):
return [l[i:i+n] for i in xrange(0, len(l), n)]
return [l[i:i+n] for i in range(0, len(l), n)]

evens = [2,4,6,8,10]
coordinates = store.select_as_coordinates('dfeq','number=evens')
Expand Down
7 changes: 3 additions & 4 deletions doc/source/remote_data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import os
import csv
from StringIO import StringIO
import pandas as pd

import numpy as np
Expand Down Expand Up @@ -49,7 +48,7 @@ Yahoo! Finance
import pandas.io.data as web
import datetime
start = datetime.datetime(2010, 1, 1)
end = datetime.datetime(2013, 01, 27)
end = datetime.datetime(2013, 1, 27)
f=web.DataReader("F", 'yahoo', start, end)
f.ix['2010-01-04']

Expand All @@ -63,7 +62,7 @@ Google Finance
import pandas.io.data as web
import datetime
start = datetime.datetime(2010, 1, 1)
end = datetime.datetime(2013, 01, 27)
end = datetime.datetime(2013, 1, 27)
f=web.DataReader("F", 'google', start, end)
f.ix['2010-01-04']

Expand All @@ -77,7 +76,7 @@ FRED
import pandas.io.data as web
import datetime
start = datetime.datetime(2010, 1, 1)
end = datetime.datetime(2013, 01, 27)
end = datetime.datetime(2013, 1, 27)
gdp=web.DataReader("GDP", "fred", start, end)
gdp.ix['2013-01-01']

Expand Down
5 changes: 5 additions & 0 deletions doc/source/reshaping.rst
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,11 @@ To encode 1-d values as an enumerated type use ``factorize``:
Note that ``factorize`` is similar to ``numpy.unique``, but differs in its
handling of NaN:

.. note::
The following ``numpy.unique`` will fail under Python 3 with a ``TypeError``
because of an ordering bug. See also
`Here <https://github.com/numpy/numpy/issues/641>`__

.. ipython:: python

pd.factorize(x, sort=True)
Expand Down
2 changes: 1 addition & 1 deletion doc/source/v0.10.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
.. ipython:: python
:suppress:

from StringIO import StringIO
from pandas.compat import StringIO

v0.10.0 (December 17, 2012)
---------------------------
Expand Down
2 changes: 1 addition & 1 deletion doc/source/v0.13.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ Experimental

nrows, ncols = 20000, 100
df1, df2, df3, df4 = [DataFrame(randn(nrows, ncols))
for _ in xrange(4)]
for _ in range(4)]

.. ipython:: python

Expand Down
2 changes: 1 addition & 1 deletion doc/source/v0.13.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ API changes
.. ipython:: python

def applied_func(col):
print "Apply function being called with:", col
print("Apply function being called with: ", col)
return col.sum()

empty = DataFrame(columns=['a', 'b'])
Expand Down
7 changes: 5 additions & 2 deletions doc/source/v0.9.0.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
.. _whatsnew_0900:

.. ipython:: python
:suppress:

from pandas.compat import StringIO

v0.9.0 (October 7, 2012)
------------------------

Expand Down Expand Up @@ -36,8 +41,6 @@ API changes

.. ipython:: python

from StringIO import StringIO

data = '0,0,1\n1,1,0\n0,1,0'
df = read_csv(StringIO(data), header=None)
df
Expand Down
6 changes: 5 additions & 1 deletion doc/source/v0.9.1.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
.. _whatsnew_0901:

.. ipython:: python
:suppress:

from pandas.compat import StringIO

v0.9.1 (November 14, 2012)
--------------------------

Expand Down Expand Up @@ -132,7 +137,6 @@ API changes

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

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


Expand Down