Skip to content

REF: de-duplicate Categorical validators #36558

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 1 commit into from
Sep 22, 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
10 changes: 2 additions & 8 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1177,13 +1177,7 @@ def _validate_where_value(self, value):
return self._validate_listlike(value)

def _validate_insert_value(self, value) -> int:
code = self.categories.get_indexer([value])
if (code == -1) and not (is_scalar(value) and isna(value)):
raise TypeError(
"cannot insert an item into a CategoricalIndex "
"that is not already an existing category"
)
return code[0]
return self._validate_fill_value(value)

def _validate_searchsorted_value(self, value):
# searchsorted is very performance sensitive. By converting codes
Expand Down Expand Up @@ -1213,7 +1207,7 @@ def _validate_fill_value(self, fill_value):
ValueError
"""

if isna(fill_value):
if is_valid_nat_for_dtype(fill_value, self.categories.dtype):
fill_value = -1
elif fill_value in self.categories:
fill_value = self._unbox_scalar(fill_value)
Expand Down
13 changes: 8 additions & 5 deletions pandas/tests/indexes/categorical/test_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,8 @@ def test_insert(self):
tm.assert_index_equal(result, expected, exact=True)

# invalid
msg = (
"cannot insert an item into a CategoricalIndex that is not "
"already an existing category"
)
with pytest.raises(TypeError, match=msg):
msg = "'fill_value=d' is not present in this Categorical's categories"
with pytest.raises(ValueError, match=msg):
ci.insert(0, "d")

# GH 18295 (test missing)
Expand All @@ -184,6 +181,12 @@ def test_insert(self):
result = CategoricalIndex(list("aabcb")).insert(1, na)
tm.assert_index_equal(result, expected)

def test_insert_na_mismatched_dtype(self):
ci = pd.CategoricalIndex([0, 1, 1])
msg = "'fill_value=NaT' is not present in this Categorical's categories"
with pytest.raises(ValueError, match=msg):
ci.insert(0, pd.NaT)

def test_delete(self):

ci = self.create_index()
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/indexing/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ def test_loc_scalar(self):
"cannot insert an item into a CategoricalIndex that is not "
"already an existing category"
)
with pytest.raises(TypeError, match=msg):
msg = "'fill_value=d' is not present in this Categorical's categories"
with pytest.raises(ValueError, match=msg):
df.loc["d", "A"] = 10
with pytest.raises(TypeError, match=msg):
with pytest.raises(ValueError, match=msg):
df.loc["d", "C"] = 10

with pytest.raises(KeyError, match="^1$"):
Expand Down