Skip to content

Commit aa6298f

Browse files
author
Thomas Dickson
authored
ERR: Cartesian product error (#36335)
1 parent 4859be9 commit aa6298f

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

pandas/core/reshape/util.py

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ def cartesian_product(X):
3939
lenX = np.fromiter((len(x) for x in X), dtype=np.intp)
4040
cumprodX = np.cumproduct(lenX)
4141

42+
if np.any(cumprodX < 0):
43+
raise ValueError("Product space too large to allocate arrays!")
44+
4245
a = np.roll(cumprodX, 1)
4346
a[0] = 1
4447

pandas/tests/reshape/test_util.py

+10
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,13 @@ def test_invalid_input(self, X):
6565

6666
with pytest.raises(TypeError, match=msg):
6767
cartesian_product(X=X)
68+
69+
def test_exceed_product_space(self):
70+
# GH31355: raise useful error when produce space is too large
71+
msg = "Product space too large to allocate arrays!"
72+
73+
with pytest.raises(ValueError, match=msg):
74+
dims = [np.arange(0, 22, dtype=np.int16) for i in range(12)] + [
75+
(np.arange(15128, dtype=np.int16)),
76+
]
77+
cartesian_product(X=dims)

0 commit comments

Comments
 (0)