Skip to content

REF: reuse can_hold_element for NumericIndex._validate_fill_value #39427

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
Jan 28, 2021
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
3 changes: 3 additions & 0 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1952,4 +1952,7 @@ def can_hold_element(dtype: np.dtype, element: Any) -> bool:
return tipo.kind == "b"
return lib.is_bool(element)

elif dtype == object:
return True

raise NotImplementedError(dtype)
2 changes: 1 addition & 1 deletion pandas/core/dtypes/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1677,7 +1677,7 @@ def _is_dtype_type(arr_or_dtype, condition) -> bool:
return condition(tipo)


def infer_dtype_from_object(dtype):
def infer_dtype_from_object(dtype) -> DtypeObj:
"""
Get a numpy dtype.type-style object for a dtype object.

Expand Down
3 changes: 3 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
from pandas.util._decorators import Appender, cache_readonly, doc

from pandas.core.dtypes.cast import (
can_hold_element,
find_common_type,
maybe_cast_to_integer_array,
maybe_promote,
Expand Down Expand Up @@ -4360,6 +4361,8 @@ def _validate_fill_value(self, value):
TypeError
If the value cannot be inserted into an array of this dtype.
"""
if not can_hold_element(self.dtype, value):
raise TypeError
return value

@final
Expand Down
6 changes: 6 additions & 0 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,12 @@ def insert(self, loc: int, item):
result._data._freq = self._get_insert_freq(loc, item)
return result

def _validate_fill_value(self, value):
"""
Convert value to be insertable to ndarray.
"""
return self._data._validate_setitem_value(value)

# --------------------------------------------------------------------
# Join/Set Methods

Expand Down
6 changes: 0 additions & 6 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,6 @@ def __reduce__(self):
d.update(self._get_attributes_dict())
return _new_DatetimeIndex, (type(self), d), None

def _validate_fill_value(self, value):
"""
Convert value to be insertable to ndarray.
"""
return self._data._validate_setitem_value(value)

def _is_comparable_dtype(self, dtype: DtypeObj) -> bool:
"""
Can we compare values of the given dtype to our own?
Expand Down
39 changes: 0 additions & 39 deletions pandas/core/indexes/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
from pandas.core.dtypes.cast import astype_nansafe
from pandas.core.dtypes.common import (
is_bool,
is_bool_dtype,
is_dtype_equal,
is_extension_array_dtype,
is_float,
is_float_dtype,
is_integer_dtype,
is_number,
is_numeric_dtype,
is_scalar,
is_signed_integer_dtype,
Expand All @@ -25,7 +23,6 @@
pandas_dtype,
)
from pandas.core.dtypes.generic import ABCSeries
from pandas.core.dtypes.missing import is_valid_nat_for_dtype, isna

import pandas.core.common as com
from pandas.core.indexes.base import Index, maybe_extract_name
Expand Down Expand Up @@ -122,42 +119,6 @@ def _shallow_copy(self, values=None, name: Hashable = lib.no_default):
return Float64Index._simple_new(values, name=name)
return super()._shallow_copy(values=values, name=name)

@doc(Index._validate_fill_value)
def _validate_fill_value(self, value):
if is_bool(value) or is_bool_dtype(value):
# force conversion to object
# so we don't lose the bools
raise TypeError
elif is_scalar(value) and isna(value):
if is_valid_nat_for_dtype(value, self.dtype):
value = self._na_value
if self.dtype.kind != "f":
# raise so that caller can cast
raise TypeError
else:
# NaT, np.datetime64("NaT"), np.timedelta64("NaT")
raise TypeError

elif is_scalar(value):
if not is_number(value):
# e.g. datetime64, timedelta64, datetime, ...
raise TypeError

elif lib.is_complex(value):
# at least until we have a ComplexIndx
raise TypeError

elif is_float(value) and self.dtype.kind != "f":
if not value.is_integer():
raise TypeError
value = int(value)

elif hasattr(value, "dtype") and value.dtype.kind in ["m", "M"]:
# TODO: if we're checking arraylike here, do so systematically
raise TypeError

return value

def _convert_tolerance(self, tolerance, target):
tolerance = np.asarray(tolerance)
if target.size != tolerance.size and tolerance.size > 1:
Expand Down