Skip to content
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

BUG: disallow scalar in CategoricalIndex construtor #38614

Merged
merged 4 commits into from
Dec 23, 2020
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
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ Bug fixes

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

- Bug in :class:`CategoricalIndex` incorrectly failing to raise ``TypeError`` when scalar data is passed (:issue:`38614`)
- Bug in ``CategoricalIndex.reindex`` failed when ``Index`` passed with elements all in category (:issue:`28690`)
-

Expand Down
6 changes: 1 addition & 5 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,7 @@ def __new__(
name = maybe_extract_name(name, data, cls)

if is_scalar(data):
# don't allow scalars
# if data is None, then categories must be provided
if data is not None or categories is None:
raise cls._scalar_data_error(data)
data = []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not actually about passing scalar data, but about passing no data (eg pd.CategoricalIndex(categories=['a', 'b'])). We generally support constructors with passing no data to create an empty (eg Series also allows this).

So I would either keep supporting this, or either it should at least be deprecated (this eg breaks dask)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #38944 to track this

raise cls._scalar_data_error(data)

data = Categorical(
data, categories=categories, ordered=ordered, dtype=dtype, copy=copy
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexes/categorical/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def test_insert(self):
tm.assert_index_equal(result, expected, exact=True)

# test empty
result = CategoricalIndex(categories=categories).insert(0, "a")
result = CategoricalIndex([], categories=categories).insert(0, "a")
expected = CategoricalIndex(["a"], categories=categories)
tm.assert_index_equal(result, expected, exact=True)

Expand Down
7 changes: 6 additions & 1 deletion pandas/tests/indexes/categorical/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@


class TestCategoricalIndexConstructors:
def test_construction_disallows_scalar(self):
msg = "must be called with a collection of some kind"
with pytest.raises(TypeError, match=msg):
CategoricalIndex(categories=list("abcd"), ordered=False)

def test_construction(self):

ci = CategoricalIndex(list("aabbca"), categories=list("abcd"), ordered=False)
Expand All @@ -20,7 +25,7 @@ def test_construction(self):
assert not result.ordered

# empty
result = CategoricalIndex(categories=categories)
result = CategoricalIndex([], categories=categories)
tm.assert_index_equal(result.categories, Index(categories))
tm.assert_numpy_array_equal(result.codes, np.array([], dtype="int8"))
assert not result.ordered
Expand Down