Skip to content

Commit

Permalink
BUG/API: Categorical constructor scalar categories (pandas-dev#16340)
Browse files Browse the repository at this point in the history
* BUG: Categorical constructor scalar categories

Categorical constructor no longer accepts scalars for categories.

Closes pandas-dev#16022
  • Loading branch information
TomAugspurger authored and stangirala committed Jun 11, 2017
1 parent 6249819 commit 0c735ec
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
5 changes: 5 additions & 0 deletions doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -110,6 +112,9 @@ Numeric
^^^^^^^


Categorical
^^^^^^^^^^^


Other
^^^^^
3 changes: 3 additions & 0 deletions pandas/core/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 5 additions & 6 deletions pandas/tests/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 0c735ec

Please sign in to comment.