From ad357c563de8b9ae7de9a255b9cae6a21785d746 Mon Sep 17 00:00:00 2001 From: Thomas Dickson Date: Sun, 13 Sep 2020 14:29:15 +0100 Subject: [PATCH 1/2] BUG GH31355 fixed by adding more relevant error message --- pandas/core/reshape/util.py | 4 ++++ pandas/tests/reshape/test_util.py | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/pandas/core/reshape/util.py b/pandas/core/reshape/util.py index a1bf3f8ee4119..d724fa9086148 100644 --- a/pandas/core/reshape/util.py +++ b/pandas/core/reshape/util.py @@ -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 diff --git a/pandas/tests/reshape/test_util.py b/pandas/tests/reshape/test_util.py index 9d074b5ade425..0acadc54cec0c 100644 --- a/pandas/tests/reshape/test_util.py +++ b/pandas/tests/reshape/test_util.py @@ -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) From c965099fd3d4a83301ce7d5c9aec3d4361acdfdc Mon Sep 17 00:00:00 2001 From: Thomas Dickson Date: Sun, 13 Sep 2020 16:43:27 +0100 Subject: [PATCH 2/2] move location of valueerror message --- pandas/core/reshape/util.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/core/reshape/util.py b/pandas/core/reshape/util.py index d724fa9086148..d2c08712abacd 100644 --- a/pandas/core/reshape/util.py +++ b/pandas/core/reshape/util.py @@ -39,9 +39,8 @@ 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) + raise ValueError("Product space too large to allocate arrays!") a = np.roll(cumprodX, 1) a[0] = 1