Skip to content

Commit fefa2bf

Browse files
committed
CLN make cartesian product faster
1 parent 2d63a71 commit fefa2bf

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

pandas/tools/util.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@ def cartesian_product(X):
1818
array([1, 2, 1, 2, 1, 2])]
1919
2020
'''
21-
lenX = map(len, X)
21+
22+
lenX = np.fromiter((len(x) for x in X), dtype=int)
2223
cumprodX = np.cumproduct(lenX)
23-
a = np.insert(cumprodX, 0, 1)
24-
b = a[-1] / a[1:]
25-
return [np.tile(np.repeat(x, b[i]),
26-
np.product(a[i]))
27-
for i, x in enumerate(X)]
2824

25+
a = np.roll(cumprodX, 1)
26+
a[0] = 1
27+
28+
b = cumprodX[-1] / cumprodX
29+
30+
return [np.tile(np.repeat(x, b[i]),
31+
np.product(a[i]))
32+
for i, x in enumerate(X)]

0 commit comments

Comments
 (0)