From 7e222bdda601efca93c85b03ddd2b18f182b033e Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Fri, 12 May 2017 08:23:44 -0500 Subject: [PATCH 1/2] BUG: Categorical constructor scalar categories Categorical constructor no longer accepts scalars for categories. Closes #16022 --- doc/source/whatsnew/v0.21.0.txt | 4 ++++ pandas/core/categorical.py | 3 +++ pandas/tests/test_categorical.py | 11 +++++------ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index f392687a0a3fd..72d762813cb66 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -109,6 +109,10 @@ Numeric ^^^^^^^ +Categorical +^^^^^^^^^^^ + +- The Categorical constructor no longer accepts a scalar for the ``categories`` keyword (:issue:`16022`) Other ^^^^^ diff --git a/pandas/core/categorical.py b/pandas/core/categorical.py index 7eb86232cbb07..edbb07b7069e9 100644 --- a/pandas/core/categorical.py +++ b/pandas/core/categorical.py @@ -533,6 +533,9 @@ def _validate_categories(cls, categories, fastpath=False): if not isinstance(categories, ABCIndexClass): dtype = None if not hasattr(categories, "dtype"): + if not is_list_like(categories): + raise TypeError("`categories` must be list-like. " + "Got {} instead".format(repr(categories))) categories = _convert_to_list_like(categories) # On categories with NaNs, int values would be converted to # float. Use "object" dtype to prevent this. diff --git a/pandas/tests/test_categorical.py b/pandas/tests/test_categorical.py index 03adf17f50300..57676be68bedf 100644 --- a/pandas/tests/test_categorical.py +++ b/pandas/tests/test_categorical.py @@ -256,12 +256,6 @@ def f(): assert len(cat.codes) == 1 assert cat.codes[0] == 0 - cat = pd.Categorical([1], categories=1) - assert len(cat.categories) == 1 - assert cat.categories[0] == 1 - assert len(cat.codes) == 1 - assert cat.codes[0] == 0 - # Catch old style constructor useage: two arrays, codes + categories # We can only catch two cases: # - when the first is an integer dtype and the second is not @@ -285,6 +279,11 @@ def f(): c = Categorical(np.array([], dtype='int64'), # noqa categories=[3, 2, 1], ordered=True) + def test_constructor_not_sequence(self): + # https://github.com/pandas-dev/pandas/issues/16022 + with pytest.raises(TypeError): + Categorical(['a', 'b'], categories='a') + def test_constructor_with_null(self): # Cannot have NaN in categories From d898b8b344f6a8bcd6beef329297a556434a64e4 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Tue, 16 May 2017 09:00:18 -0500 Subject: [PATCH 2/2] fixup! BUG: Categorical constructor scalar categories --- doc/source/whatsnew/v0.21.0.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 72d762813cb66..afee6a0c2025e 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -36,6 +36,8 @@ Backwards incompatible API changes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - Support has been dropped for Python 3.4 (:issue:`15251`) +- The Categorical constructor no longer accepts a scalar for the ``categories`` keyword. (:issue:`16022`) + .. _whatsnew_0210.api: @@ -112,7 +114,6 @@ Numeric Categorical ^^^^^^^^^^^ -- The Categorical constructor no longer accepts a scalar for the ``categories`` keyword (:issue:`16022`) Other ^^^^^