From 06296b17651764658188935a3441a896fc2ea25d Mon Sep 17 00:00:00 2001 From: phaebz Date: Sun, 2 Feb 2014 11:03:38 +0100 Subject: [PATCH 1/2] DOC: Py2/3 examples compat (datetime, print, StringIO/BytesIO, range) --- doc/source/comparison_with_r.rst | 4 ++-- doc/source/enhancingperf.rst | 2 +- doc/source/io.rst | 14 ++++++++------ doc/source/remote_data.rst | 7 +++---- doc/source/v0.10.0.txt | 2 +- doc/source/v0.13.0.txt | 2 +- doc/source/v0.13.1.txt | 2 +- doc/source/v0.9.0.txt | 7 +++++-- doc/source/v0.9.1.txt | 6 +++++- 9 files changed, 27 insertions(+), 19 deletions(-) diff --git a/doc/source/comparison_with_r.rst b/doc/source/comparison_with_r.rst index a5b8fabac11ac..0c011a76a3c96 100644 --- a/doc/source/comparison_with_r.rst +++ b/doc/source/comparison_with_r.rst @@ -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|_ @@ -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 diff --git a/doc/source/enhancingperf.rst b/doc/source/enhancingperf.rst index 066fcce64c5ac..34166343817a4 100644 --- a/doc/source/enhancingperf.rst +++ b/doc/source/enhancingperf.rst @@ -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 diff --git a/doc/source/io.rst b/doc/source/io.rst index 5f114ec3d3c08..34af31747ca4a 100644 --- a/doc/source/io.rst +++ b/doc/source/io.rst @@ -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 @@ -58,6 +58,11 @@ The corresponding ``writer`` functions are object methods that are accessed like * :ref:`to_clipboard` * :ref:`to_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 @@ -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)) @@ -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] @@ -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()) @@ -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') diff --git a/doc/source/remote_data.rst b/doc/source/remote_data.rst index 535674721a575..16edf64802908 100644 --- a/doc/source/remote_data.rst +++ b/doc/source/remote_data.rst @@ -7,7 +7,6 @@ import os import csv - from StringIO import StringIO import pandas as pd import numpy as np @@ -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'] @@ -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'] @@ -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'] diff --git a/doc/source/v0.10.0.txt b/doc/source/v0.10.0.txt index 2e59c420fbd01..93ab3b912030d 100644 --- a/doc/source/v0.10.0.txt +++ b/doc/source/v0.10.0.txt @@ -3,7 +3,7 @@ .. ipython:: python :suppress: - from StringIO import StringIO + from pandas.compat import StringIO v0.10.0 (December 17, 2012) --------------------------- diff --git a/doc/source/v0.13.0.txt b/doc/source/v0.13.0.txt index 4f06b6ea8369e..ac0a14f45b69e 100644 --- a/doc/source/v0.13.0.txt +++ b/doc/source/v0.13.0.txt @@ -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 diff --git a/doc/source/v0.13.1.txt b/doc/source/v0.13.1.txt index 9b39be08d0984..b48f555f9691a 100644 --- a/doc/source/v0.13.1.txt +++ b/doc/source/v0.13.1.txt @@ -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']) diff --git a/doc/source/v0.9.0.txt b/doc/source/v0.9.0.txt index b0c2c2455ab77..2b385a7e7b8f0 100644 --- a/doc/source/v0.9.0.txt +++ b/doc/source/v0.9.0.txt @@ -1,5 +1,10 @@ .. _whatsnew_0900: +.. ipython:: python + :suppress: + + from pandas.compat import StringIO + v0.9.0 (October 7, 2012) ------------------------ @@ -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 diff --git a/doc/source/v0.9.1.txt b/doc/source/v0.9.1.txt index 7de000c255d4c..6718a049a0ab9 100644 --- a/doc/source/v0.9.1.txt +++ b/doc/source/v0.9.1.txt @@ -1,5 +1,10 @@ .. _whatsnew_0901: +.. ipython:: python + :suppress: + + from pandas.compat import StringIO + v0.9.1 (November 14, 2012) -------------------------- @@ -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()}) From 8cef96bc1fdedac44e59f291241442caa84082a7 Mon Sep 17 00:00:00 2001 From: phaebz Date: Tue, 4 Feb 2014 08:12:01 +0100 Subject: [PATCH 2/2] Note for failing numpy.unique and fix for pandas.Series NameError --- doc/source/comparison_with_r.rst | 2 +- doc/source/reshaping.rst | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/doc/source/comparison_with_r.rst b/doc/source/comparison_with_r.rst index 0c011a76a3c96..253eafb36653f 100644 --- a/doc/source/comparison_with_r.rst +++ b/doc/source/comparison_with_r.rst @@ -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 `. diff --git a/doc/source/reshaping.rst b/doc/source/reshaping.rst index ce8f0013e9268..8f0686dedb9d6 100644 --- a/doc/source/reshaping.rst +++ b/doc/source/reshaping.rst @@ -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 `__ + .. ipython:: python pd.factorize(x, sort=True)