Skip to content

Commit 0c735ec

Browse files
TomAugspurgerstangirala
authored andcommitted
BUG/API: Categorical constructor scalar categories (pandas-dev#16340)
* BUG: Categorical constructor scalar categories Categorical constructor no longer accepts scalars for categories. Closes pandas-dev#16022
1 parent 6249819 commit 0c735ec

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

doc/source/whatsnew/v0.21.0.txt

+5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ Backwards incompatible API changes
3737
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3838

3939
- Support has been dropped for Python 3.4 (:issue:`15251`)
40+
- The Categorical constructor no longer accepts a scalar for the ``categories`` keyword. (:issue:`16022`)
41+
4042

4143
.. _whatsnew_0210.api:
4244

@@ -110,6 +112,9 @@ Numeric
110112
^^^^^^^
111113

112114

115+
Categorical
116+
^^^^^^^^^^^
117+
113118

114119
Other
115120
^^^^^

pandas/core/categorical.py

+3
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,9 @@ def _validate_categories(cls, categories, fastpath=False):
533533
if not isinstance(categories, ABCIndexClass):
534534
dtype = None
535535
if not hasattr(categories, "dtype"):
536+
if not is_list_like(categories):
537+
raise TypeError("`categories` must be list-like. "
538+
"Got {} instead".format(repr(categories)))
536539
categories = _convert_to_list_like(categories)
537540
# On categories with NaNs, int values would be converted to
538541
# float. Use "object" dtype to prevent this.

pandas/tests/test_categorical.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,6 @@ def f():
256256
assert len(cat.codes) == 1
257257
assert cat.codes[0] == 0
258258

259-
cat = pd.Categorical([1], categories=1)
260-
assert len(cat.categories) == 1
261-
assert cat.categories[0] == 1
262-
assert len(cat.codes) == 1
263-
assert cat.codes[0] == 0
264-
265259
# Catch old style constructor useage: two arrays, codes + categories
266260
# We can only catch two cases:
267261
# - when the first is an integer dtype and the second is not
@@ -285,6 +279,11 @@ def f():
285279
c = Categorical(np.array([], dtype='int64'), # noqa
286280
categories=[3, 2, 1], ordered=True)
287281

282+
def test_constructor_not_sequence(self):
283+
# https://github.com/pandas-dev/pandas/issues/16022
284+
with pytest.raises(TypeError):
285+
Categorical(['a', 'b'], categories='a')
286+
288287
def test_constructor_with_null(self):
289288

290289
# Cannot have NaN in categories

0 commit comments

Comments
 (0)