From 9c9a897f603a3fe75f7a493b7cba433bb2e6a8fb Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 21 Dec 2020 07:45:14 -0800 Subject: [PATCH 1/2] BUG: disallow scalar in CategoricalIndex construtor --- pandas/core/indexes/category.py | 9 ++------- pandas/tests/indexes/categorical/test_category.py | 2 +- pandas/tests/indexes/categorical/test_constructors.py | 7 ++++++- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index 76b1c061cc827..2e3cc47a98993 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -188,13 +188,8 @@ def __new__( name = maybe_extract_name(name, data, cls) - if not is_categorical_dtype(data): - # don't allow scalars - # if data is None, then categories must be provided - if is_scalar(data): - if data is not None or categories is None: - raise cls._scalar_data_error(data) - data = [] + if is_scalar(data): + raise cls._scalar_data_error(data) assert isinstance(dtype, CategoricalDtype), dtype data = extract_array(data, extract_numpy=True) diff --git a/pandas/tests/indexes/categorical/test_category.py b/pandas/tests/indexes/categorical/test_category.py index 3bab57e1d265e..4a4fe0a051de1 100644 --- a/pandas/tests/indexes/categorical/test_category.py +++ b/pandas/tests/indexes/categorical/test_category.py @@ -91,7 +91,7 @@ def test_insert(self): tm.assert_index_equal(result, expected, exact=True) # test empty - result = CategoricalIndex(categories=categories).insert(0, "a") + result = CategoricalIndex([], categories=categories).insert(0, "a") expected = CategoricalIndex(["a"], categories=categories) tm.assert_index_equal(result, expected, exact=True) diff --git a/pandas/tests/indexes/categorical/test_constructors.py b/pandas/tests/indexes/categorical/test_constructors.py index ee3f85da22781..aeb31b710ddbb 100644 --- a/pandas/tests/indexes/categorical/test_constructors.py +++ b/pandas/tests/indexes/categorical/test_constructors.py @@ -6,6 +6,11 @@ class TestCategoricalIndexConstructors: + def test_construction_disallows_scalar(self): + msg = "must be called with a collection of some kind" + with pytest.raises(TypeError, match=msg): + CategoricalIndex(categories=list("abcd"), ordered=False) + def test_construction(self): ci = CategoricalIndex(list("aabbca"), categories=list("abcd"), ordered=False) @@ -20,7 +25,7 @@ def test_construction(self): assert not result.ordered # empty - result = CategoricalIndex(categories=categories) + result = CategoricalIndex([], categories=categories) tm.assert_index_equal(result.categories, Index(categories)) tm.assert_numpy_array_equal(result.codes, np.array([], dtype="int8")) assert not result.ordered From 86fd04c92acb7cb3d7031497c4b823a6d2c20862 Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 22 Dec 2020 13:19:22 -0800 Subject: [PATCH 2/2] whatsnew --- doc/source/whatsnew/v1.3.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index fbd2c2b5345fc..3a8fa7798a2d5 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -170,7 +170,7 @@ Bug fixes Categorical ^^^^^^^^^^^ - +- Bug in :class:`CategoricalIndex` incorrectly failing to raise ``TypeError`` when scalar data is passed (:issue:`38614`) - Bug in ``CategoricalIndex.reindex`` failed when ``Index`` passed with elements all in category (:issue:`28690`) -