Skip to content

BUG: Categorical constructor scalar categories #16340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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:

Expand Down Expand Up @@ -109,6 +111,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