From bc7a2f1423f699d421c6dbd73360d2d2afc3eeb2 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Sat, 7 Mar 2020 08:23:01 -0800 Subject: [PATCH 1/2] TST: fix test creating invalid CategoricalBlock --- pandas/core/internals/blocks.py | 4 ++++ pandas/tests/internals/test_internals.py | 14 ++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index c088b7020927b..7d035a162115b 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -1745,6 +1745,10 @@ def __init__(self, values, placement, ndim=None): values = self._maybe_coerce_values(values) super().__init__(values, placement, ndim) + if self.ndim == 2 and len(self.mgr_locs) != 1: + # TODO(2DEA): check unnecessary with 2D EAs + raise ValueError("block.size != values.size") + def _maybe_coerce_values(self, values): """ Unbox to an extension array. diff --git a/pandas/tests/internals/test_internals.py b/pandas/tests/internals/test_internals.py index a569726e9a22a..960852329cdd0 100644 --- a/pandas/tests/internals/test_internals.py +++ b/pandas/tests/internals/test_internals.py @@ -207,7 +207,6 @@ def setup_method(self, method): self.cblock = create_block("complex", [7]) self.oblock = create_block("object", [1, 3]) self.bool_block = create_block("bool", [5]) - self.int_block = create_block("int", [6]) def test_constructor(self): int32block = create_block("i4", [0]) @@ -541,6 +540,13 @@ def _compare(old_mgr, new_mgr): assert new_mgr.get("g").dtype == np.float64 assert new_mgr.get("h").dtype == np.float16 + def test_invalid_ea_block(self): + with pytest.raises(ValueError, match="block.size != values.size"): + create_mgr("a: category; b: category") + + with pytest.raises(ValueError, match="block.size != values.size"): + create_mgr("a: category2; b: category2") + def test_interleave(self): # self @@ -553,14 +559,10 @@ def test_interleave(self): # will be converted according the actual dtype of the underlying mgr = create_mgr("a: category") assert mgr.as_array().dtype == "i8" - mgr = create_mgr("a: category; b: category") - assert mgr.as_array().dtype == "i8" mgr = create_mgr("a: category; b: category2") assert mgr.as_array().dtype == "object" mgr = create_mgr("a: category2") assert mgr.as_array().dtype == "object" - mgr = create_mgr("a: category2; b: category2") - assert mgr.as_array().dtype == "object" # combinations mgr = create_mgr("a: f8") @@ -703,7 +705,7 @@ def test_equals(self): "a:i8;b:f8", # basic case "a:i8;b:f8;c:c8;d:b", # many types "a:i8;e:dt;f:td;g:string", # more types - "a:i8;b:category;c:category2;d:category2", # categories + "a:i8;b:category;c:category2", # categories "c:sparse;d:sparse_na;b:f8", # sparse ], ) From 913cf7eae0df6c1c0ba2aad09f4751f07423a981 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 10 Mar 2020 19:49:04 -0700 Subject: [PATCH 2/2] ValueError->AssertionError --- pandas/core/internals/blocks.py | 2 +- pandas/tests/internals/test_internals.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 330ca7db8cdbd..db28e09c8ac2d 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -1748,7 +1748,7 @@ def __init__(self, values, placement, ndim=None): if self.ndim == 2 and len(self.mgr_locs) != 1: # TODO(2DEA): check unnecessary with 2D EAs - raise ValueError("block.size != values.size") + raise AssertionError("block.size != values.size") def _maybe_coerce_values(self, values): """ diff --git a/pandas/tests/internals/test_internals.py b/pandas/tests/internals/test_internals.py index 960852329cdd0..1a7d5839d9a11 100644 --- a/pandas/tests/internals/test_internals.py +++ b/pandas/tests/internals/test_internals.py @@ -541,10 +541,10 @@ def _compare(old_mgr, new_mgr): assert new_mgr.get("h").dtype == np.float16 def test_invalid_ea_block(self): - with pytest.raises(ValueError, match="block.size != values.size"): + with pytest.raises(AssertionError, match="block.size != values.size"): create_mgr("a: category; b: category") - with pytest.raises(ValueError, match="block.size != values.size"): + with pytest.raises(AssertionError, match="block.size != values.size"): create_mgr("a: category2; b: category2") def test_interleave(self):