Skip to content

Commit e2785f7

Browse files
authored
REF: de-duplicate Categorical validators (#36558)
1 parent 9e1bd7c commit e2785f7

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

pandas/core/arrays/categorical.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -1177,13 +1177,7 @@ def _validate_where_value(self, value):
11771177
return self._validate_listlike(value)
11781178

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

11881182
def _validate_searchsorted_value(self, value):
11891183
# searchsorted is very performance sensitive. By converting codes
@@ -1213,7 +1207,7 @@ def _validate_fill_value(self, fill_value):
12131207
ValueError
12141208
"""
12151209

1216-
if isna(fill_value):
1210+
if is_valid_nat_for_dtype(fill_value, self.categories.dtype):
12171211
fill_value = -1
12181212
elif fill_value in self.categories:
12191213
fill_value = self._unbox_scalar(fill_value)

pandas/tests/indexes/categorical/test_category.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,8 @@ def test_insert(self):
171171
tm.assert_index_equal(result, expected, exact=True)
172172

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

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

184+
def test_insert_na_mismatched_dtype(self):
185+
ci = pd.CategoricalIndex([0, 1, 1])
186+
msg = "'fill_value=NaT' is not present in this Categorical's categories"
187+
with pytest.raises(ValueError, match=msg):
188+
ci.insert(0, pd.NaT)
189+
187190
def test_delete(self):
188191

189192
ci = self.create_index()

pandas/tests/indexing/test_categorical.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,10 @@ def test_loc_scalar(self):
7676
"cannot insert an item into a CategoricalIndex that is not "
7777
"already an existing category"
7878
)
79-
with pytest.raises(TypeError, match=msg):
79+
msg = "'fill_value=d' is not present in this Categorical's categories"
80+
with pytest.raises(ValueError, match=msg):
8081
df.loc["d", "A"] = 10
81-
with pytest.raises(TypeError, match=msg):
82+
with pytest.raises(ValueError, match=msg):
8283
df.loc["d", "C"] = 10
8384

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

0 commit comments

Comments
 (0)