Skip to content

REF: _assert_can_do_op -> _validate_scalar #36367

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 15, 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
8 changes: 5 additions & 3 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2250,7 +2250,7 @@ def fillna(self, value=None, downcast=None):
DataFrame.fillna : Fill NaN values of a DataFrame.
Series.fillna : Fill NaN Values of a Series.
"""
self._assert_can_do_op(value)
value = self._validate_scalar(value)
if self.hasnans:
result = self.putmask(self._isnan, value)
if downcast is None:
Expand Down Expand Up @@ -4053,12 +4053,14 @@ def _validate_fill_value(self, value):
"""
return value

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

@property
def _has_complex_internals(self) -> bool:
Expand Down
5 changes: 3 additions & 2 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,8 +380,9 @@ def _isnan(self):

@doc(Index.fillna)
def fillna(self, value, downcast=None):
self._assert_can_do_op(value)
return CategoricalIndex(self._data.fillna(value), name=self.name)
value = self._validate_scalar(value)
cat = self._data.fillna(value)
return type(self)._simple_new(cat, name=self.name)

@cache_readonly
def _engine(self):
Expand Down