From 0c735ecae9c33d102dc13a508d05e8ad57200ec9 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Tue, 16 May 2017 12:33:58 -0500 Subject: [PATCH] BUG/API: Categorical constructor scalar categories (#16340) * BUG: Categorical constructor scalar categories Categorical constructor no longer accepts scalars for categories. Closes #16022 --- doc/source/whatsnew/v0.21.0.txt | 5 +++++ pandas/core/categorical.py | 3 +++ pandas/tests/test_categorical.py | 11 +++++------ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 1a500bdc65ce30..90f38620fdfcff 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -37,6 +37,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: @@ -110,6 +112,9 @@ Numeric ^^^^^^^ +Categorical +^^^^^^^^^^^ + Other ^^^^^ diff --git a/pandas/core/categorical.py b/pandas/core/categorical.py index 7eb86232cbb073..edbb07b7069e9a 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 03adf17f50300e..57676be68bedfb 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