Skip to content

Commit 8ac0e11

Browse files
committed
Merge pull request #4384 from jtratner/better-python3-compat
CLN/ENH/BLD: Remove need for 2to3 for Python 3.
2 parents 7e896ea + fa16b95 commit 8ac0e11

File tree

198 files changed

+3667
-3150
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

198 files changed

+3667
-3150
lines changed

LICENSES/SIX

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
six license (substantial portions used in the python 3 compatibility module)
2+
===========================================================================
3+
Copyright (c) 2010-2013 Benjamin Peterson
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
#
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
#
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

bench/alignment.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# Setup
2+
from pandas.compat import range, lrange
23
import numpy as np
34
import pandas
45
import la
56
N = 1000
67
K = 50
78
arr1 = np.random.randn(N, K)
89
arr2 = np.random.randn(N, K)
9-
idx1 = range(N)
10-
idx2 = range(K)
10+
idx1 = lrange(N)
11+
idx2 = lrange(K)
1112

1213
# pandas
1314
dma1 = pandas.DataFrame(arr1, idx1, idx2)

bench/bench_get_put_value.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from pandas import *
22
from pandas.util.testing import rands
3+
from pandas.compat import range
34

45
N = 1000
56
K = 50
67

78

89
def _random_index(howmany):
9-
return Index([rands(10) for _ in xrange(howmany)])
10+
return Index([rands(10) for _ in range(howmany)])
1011

1112
df = DataFrame(np.random.randn(N, K), index=_random_index(N),
1213
columns=_random_index(K))

bench/bench_groupby.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
from pandas import *
22
from pandas.util.testing import rands
3+
from pandas.compat import range
34

45
import string
56
import random
67

78
k = 20000
89
n = 10
910

10-
foo = np.tile(np.array([rands(10) for _ in xrange(k)], dtype='O'), n)
11+
foo = np.tile(np.array([rands(10) for _ in range(k)], dtype='O'), n)
1112
foo2 = list(foo)
1213
random.shuffle(foo)
1314
random.shuffle(foo2)

bench/bench_join_panel.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def reindex_on_axis(panels, axis, axis_reindex):
3535
# concatenate values
3636
try:
3737
values = np.concatenate([p.values for p in panels], axis=1)
38-
except (Exception), detail:
38+
except Exception as detail:
3939
raise Exception("cannot append values that dont' match dimensions! -> [%s] %s"
4040
% (','.join(["%s" % p for p in panels]), str(detail)))
4141
# pm('append - create_panel')

bench/bench_khash_dict.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
"""
22
Some comparisons of khash.h to Python dict
33
"""
4+
from __future__ import print_function
45

56
import numpy as np
67
import os
78

89
from vbench.api import Benchmark
910
from pandas.util.testing import rands
11+
from pandas.compat import range
1012
import pandas._tseries as lib
1113
import pandas._sandbox as sbx
1214
import time
@@ -22,7 +24,7 @@ def object_test_data(n):
2224

2325

2426
def string_test_data(n):
25-
return np.array([rands(10) for _ in xrange(n)], dtype='O')
27+
return np.array([rands(10) for _ in range(n)], dtype='O')
2628

2729

2830
def int_test_data(n):
@@ -50,7 +52,7 @@ def f():
5052

5153
def _timeit(f, iterations=10):
5254
start = time.time()
53-
for _ in xrange(iterations):
55+
for _ in range(iterations):
5456
foo = f()
5557
elapsed = time.time() - start
5658
return elapsed
@@ -73,8 +75,8 @@ def lookup_khash(values):
7375

7476

7577
def leak(values):
76-
for _ in xrange(100):
77-
print proc.get_memory_info()
78+
for _ in range(100):
79+
print(proc.get_memory_info())
7880
table = lookup_khash(values)
7981
# table.destroy()
8082

bench/bench_merge.py

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
import random
2+
import gc
3+
import time
14
from pandas import *
5+
from pandas.compat import range, lrange, StringIO
26
from pandas.util.testing import rands
3-
import random
47

58
N = 10000
69
ngroups = 10
710

811

912
def get_test_data(ngroups=100, n=N):
10-
unique_groups = range(ngroups)
13+
unique_groups = lrange(ngroups)
1114
arr = np.asarray(np.tile(unique_groups, n / ngroups), dtype=object)
1215

1316
if len(arr) < n:
@@ -28,14 +31,10 @@ def get_test_data(ngroups=100, n=N):
2831
# 'value' : np.random.randn(N // 10)})
2932
# result = merge.merge(df, df2, on='key2')
3033

31-
from collections import defaultdict
32-
import gc
33-
import time
34-
from pandas.util.testing import rands
3534
N = 10000
3635

37-
indices = np.array([rands(10) for _ in xrange(N)], dtype='O')
38-
indices2 = np.array([rands(10) for _ in xrange(N)], dtype='O')
36+
indices = np.array([rands(10) for _ in range(N)], dtype='O')
37+
indices2 = np.array([rands(10) for _ in range(N)], dtype='O')
3938
key = np.tile(indices[:8000], 10)
4039
key2 = np.tile(indices2[:8000], 10)
4140

@@ -55,7 +54,7 @@ def get_test_data(ngroups=100, n=N):
5554
f = lambda: merge(left, right, how=join_method, sort=sort)
5655
gc.disable()
5756
start = time.time()
58-
for _ in xrange(niter):
57+
for _ in range(niter):
5958
f()
6059
elapsed = (time.time() - start) / niter
6160
gc.enable()
@@ -65,7 +64,6 @@ def get_test_data(ngroups=100, n=N):
6564

6665

6766
# R results
68-
from StringIO import StringIO
6967
# many to one
7068
r_results = read_table(StringIO(""" base::merge plyr data.table
7169
inner 0.2475 0.1183 0.1100
@@ -93,7 +91,6 @@ def get_test_data(ngroups=100, n=N):
9391

9492
# many to many
9593

96-
from StringIO import StringIO
9794
# many to one
9895
r_results = read_table(StringIO("""base::merge plyr data.table
9996
inner 0.4610 0.1276 0.1269

bench/bench_merge_sqlite.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
import time
55
from pandas import DataFrame
66
from pandas.util.testing import rands
7+
from pandas.compat import range, zip
78
import random
89

910
N = 10000
1011

11-
indices = np.array([rands(10) for _ in xrange(N)], dtype='O')
12-
indices2 = np.array([rands(10) for _ in xrange(N)], dtype='O')
12+
indices = np.array([rands(10) for _ in range(N)], dtype='O')
13+
indices2 = np.array([rands(10) for _ in range(N)], dtype='O')
1314
key = np.tile(indices[:8000], 10)
1415
key2 = np.tile(indices2[:8000], 10)
1516

@@ -67,7 +68,7 @@
6768
g = lambda: conn.execute(sql) # list fetches results
6869
gc.disable()
6970
start = time.time()
70-
# for _ in xrange(niter):
71+
# for _ in range(niter):
7172
g()
7273
elapsed = (time.time() - start) / niter
7374
gc.enable()

bench/bench_sparse.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from pandas import *
55
import pandas.core.sparse as spm
6+
import pandas.compat as compat
67
reload(spm)
78
from pandas.core.sparse import *
89

@@ -41,7 +42,7 @@
4142

4243
def new_data_like(sdf):
4344
new_data = {}
44-
for col, series in sdf.iteritems():
45+
for col, series in compat.iteritems(sdf):
4546
new_data[col] = SparseSeries(np.random.randn(len(series.sp_values)),
4647
index=sdf.index,
4748
sparse_index=series.sp_index,

bench/bench_take_indexing.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
from __future__ import print_function
12
import numpy as np
23

34
from pandas import *
45
import pandas._tseries as lib
56

67
from pandas import DataFrame
78
import timeit
9+
from pandas.compat import zip
810

911
setup = """
1012
from pandas import Series
@@ -35,7 +37,7 @@ def _timeit(stmt, size, k=5, iters=1000):
3537
return timer.timeit(n) / n
3638

3739
for sz, its in zip(sizes, iters):
38-
print sz
40+
print(sz)
3941
fancy_2d.append(_timeit('arr[indexer]', sz, iters=its))
4042
take_2d.append(_timeit('arr.take(indexer, axis=0)', sz, iters=its))
4143
cython_2d.append(_timeit('lib.take_axis0(arr, indexer)', sz, iters=its))
@@ -44,7 +46,7 @@ def _timeit(stmt, size, k=5, iters=1000):
4446
'take': take_2d,
4547
'cython': cython_2d})
4648

47-
print df
49+
print(df)
4850

4951
from pandas.rpy.common import r
5052
r('mat <- matrix(rnorm(50000), nrow=10000, ncol=5)')

bench/bench_unique.py

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
from __future__ import print_function
12
from pandas import *
23
from pandas.util.testing import rands
4+
from pandas.compat import range, zip
35
import pandas._tseries as lib
46
import numpy as np
57
import matplotlib.pyplot as plt
68

79
N = 50000
810
K = 10000
911

10-
groups = np.array([rands(10) for _ in xrange(K)], dtype='O')
11-
groups2 = np.array([rands(10) for _ in xrange(K)], dtype='O')
12+
groups = np.array([rands(10) for _ in range(K)], dtype='O')
13+
groups2 = np.array([rands(10) for _ in range(K)], dtype='O')
1214

1315
labels = np.tile(groups, N // K)
1416
labels2 = np.tile(groups2, N // K)
@@ -20,7 +22,7 @@ def timeit(f, niter):
2022
import time
2123
gc.disable()
2224
start = time.time()
23-
for _ in xrange(niter):
25+
for _ in range(niter):
2426
f()
2527
elapsed = (time.time() - start) / niter
2628
gc.enable()
@@ -75,9 +77,8 @@ def algo3_sort():
7577

7678

7779
def f():
78-
from itertools import izip
7980
# groupby sum
80-
for k, v in izip(x, data):
81+
for k, v in zip(x, data):
8182
try:
8283
counts[k] += v
8384
except KeyError:
@@ -128,7 +129,7 @@ def algo4():
128129
# N = 10000000
129130
# K = 500000
130131

131-
# groups = np.array([rands(10) for _ in xrange(K)], dtype='O')
132+
# groups = np.array([rands(10) for _ in range(K)], dtype='O')
132133

133134
# labels = np.tile(groups, N // K)
134135
data = np.random.randn(N)
@@ -232,11 +233,11 @@ def hash_bench():
232233
khash_hint = []
233234
khash_nohint = []
234235
for K in Ks:
235-
print K
236-
# groups = np.array([rands(10) for _ in xrange(K)])
236+
print(K)
237+
# groups = np.array([rands(10) for _ in range(K)])
237238
# labels = np.tile(groups, N // K).astype('O')
238239

239-
groups = np.random.randint(0, 100000000000L, size=K)
240+
groups = np.random.randint(0, long(100000000000), size=K)
240241
labels = np.tile(groups, N // K)
241242
dict_based.append(timeit(lambda: dict_unique(labels, K), 20))
242243
khash_nohint.append(timeit(lambda: khash_unique_int64(labels, K), 20))
@@ -245,11 +246,11 @@ def hash_bench():
245246

246247
# memory, hard to get
247248
# dict_based.append(np.mean([dict_unique(labels, K, memory=True)
248-
# for _ in xrange(10)]))
249+
# for _ in range(10)]))
249250
# khash_nohint.append(np.mean([khash_unique(labels, K, memory=True)
250-
# for _ in xrange(10)]))
251+
# for _ in range(10)]))
251252
# khash_hint.append(np.mean([khash_unique(labels, K, size_hint=True, memory=True)
252-
# for _ in xrange(10)]))
253+
# for _ in range(10)]))
253254

254255
# dict_based_sort.append(timeit(lambda: dict_unique(labels, K,
255256
# sort=True), 10))

bench/better_unique.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
from __future__ import print_function
12
from pandas import DataFrame
3+
from pandas.compat import range, zip
24
import timeit
35

46
setup = """
57
from pandas import Series
68
import pandas._tseries as _tseries
9+
from pandas.compat import range
710
import random
811
import numpy as np
912
@@ -48,11 +51,11 @@ def get_test_data(ngroups=100, n=tot):
4851
numpy_timer = timeit.Timer(stmt='np.unique(arr)',
4952
setup=setup % sz)
5053

51-
print n
54+
print(n)
5255
numpy_result = numpy_timer.timeit(number=n) / n
5356
wes_result = wes_timer.timeit(number=n) / n
5457

55-
print 'Groups: %d, NumPy: %s, Wes: %s' % (sz, numpy_result, wes_result)
58+
print('Groups: %d, NumPy: %s, Wes: %s' % (sz, numpy_result, wes_result))
5659

5760
wes.append(wes_result)
5861
numpy.append(numpy_result)

bench/io_roundtrip.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1+
from __future__ import print_function
12
import time
23
import os
34
import numpy as np
45

56
import la
67
import pandas
8+
from pandas.compat import range
79
from pandas import datetools, DateRange
810

911

1012
def timeit(f, iterations):
1113
start = time.clock()
1214

13-
for i in xrange(iterations):
15+
for i in range(iterations):
1416
f()
1517

1618
return time.clock() - start
@@ -54,11 +56,11 @@ def rountrip_archive(N, K=50, iterations=10):
5456

5557
pandas_f = lambda: pandas_roundtrip(filename_pandas, dma, dma)
5658
pandas_time = timeit(pandas_f, iterations) / iterations
57-
print 'pandas (HDF5) %7.4f seconds' % pandas_time
59+
print('pandas (HDF5) %7.4f seconds' % pandas_time)
5860

5961
pickle_f = lambda: pandas_roundtrip(filename_pandas, dma, dma)
6062
pickle_time = timeit(pickle_f, iterations) / iterations
61-
print 'pandas (pickle) %7.4f seconds' % pickle_time
63+
print('pandas (pickle) %7.4f seconds' % pickle_time)
6264

6365
# print 'Numpy (npz) %7.4f seconds' % numpy_time
6466
# print 'larry (HDF5) %7.4f seconds' % larry_time

0 commit comments

Comments
 (0)