Skip to content

ERR: Cartesian product error #36335

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
merged 3 commits into from
Sep 13, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions pandas/core/reshape/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def cartesian_product(X):
lenX = np.fromiter((len(x) for x in X), dtype=np.intp)
cumprodX = np.cumproduct(lenX)

msg = "Product space too large to allocate arrays!"
if np.any(cumprodX < 0):
raise ValueError(msg)

a = np.roll(cumprodX, 1)
a[0] = 1

Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/reshape/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,13 @@ def test_invalid_input(self, X):

with pytest.raises(TypeError, match=msg):
cartesian_product(X=X)

def test_exceed_product_space(self):
# GH31355: raise useful error when produce space is too large
msg = "Product space too large to allocate arrays!"

with pytest.raises(ValueError, match=msg):
dims = [np.arange(0, 22, dtype=np.int16) for i in range(12)] + [
(np.arange(15128, dtype=np.int16)),
]
cartesian_product(X=dims)