Skip to content

Commit 744be1f

Browse files
jbrockmendelKevin D Smith
authored and
Kevin D Smith
committed
REF: _assert_can_do_op -> _validate_scalar (pandas-dev#36367)
1 parent 8075df1 commit 744be1f

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

pandas/core/indexes/base.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -2250,7 +2250,7 @@ def fillna(self, value=None, downcast=None):
22502250
DataFrame.fillna : Fill NaN values of a DataFrame.
22512251
Series.fillna : Fill NaN Values of a Series.
22522252
"""
2253-
self._assert_can_do_op(value)
2253+
value = self._validate_scalar(value)
22542254
if self.hasnans:
22552255
result = self.putmask(self._isnan, value)
22562256
if downcast is None:
@@ -4053,12 +4053,14 @@ def _validate_fill_value(self, value):
40534053
"""
40544054
return value
40554055

4056-
def _assert_can_do_op(self, value):
4056+
def _validate_scalar(self, value):
40574057
"""
4058-
Check value is valid for scalar op.
4058+
Check that this is a scalar value that we can use for setitem-like
4059+
operations without changing dtype.
40594060
"""
40604061
if not is_scalar(value):
40614062
raise TypeError(f"'value' must be a scalar, passed: {type(value).__name__}")
4063+
return value
40624064

40634065
@property
40644066
def _has_complex_internals(self) -> bool:

pandas/core/indexes/category.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,9 @@ def _isnan(self):
380380

381381
@doc(Index.fillna)
382382
def fillna(self, value, downcast=None):
383-
self._assert_can_do_op(value)
384-
return CategoricalIndex(self._data.fillna(value), name=self.name)
383+
value = self._validate_scalar(value)
384+
cat = self._data.fillna(value)
385+
return type(self)._simple_new(cat, name=self.name)
385386

386387
@cache_readonly
387388
def _engine(self):

0 commit comments

Comments
 (0)