Skip to content

Commit ea906c3

Browse files
authored
REF: reuse can_hold_element for NumericIndex._validate_fill_value (#39427)
1 parent c750fb4 commit ea906c3

File tree

6 files changed

+13
-46
lines changed

6 files changed

+13
-46
lines changed

pandas/core/dtypes/cast.py

+3
Original file line numberDiff line numberDiff line change
@@ -1952,4 +1952,7 @@ def can_hold_element(dtype: np.dtype, element: Any) -> bool:
19521952
return tipo.kind == "b"
19531953
return lib.is_bool(element)
19541954

1955+
elif dtype == object:
1956+
return True
1957+
19551958
raise NotImplementedError(dtype)

pandas/core/dtypes/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1677,7 +1677,7 @@ def _is_dtype_type(arr_or_dtype, condition) -> bool:
16771677
return condition(tipo)
16781678

16791679

1680-
def infer_dtype_from_object(dtype):
1680+
def infer_dtype_from_object(dtype) -> DtypeObj:
16811681
"""
16821682
Get a numpy dtype.type-style object for a dtype object.
16831683

pandas/core/indexes/base.py

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from pandas.util._decorators import Appender, cache_readonly, doc
4141

4242
from pandas.core.dtypes.cast import (
43+
can_hold_element,
4344
find_common_type,
4445
infer_dtype_from,
4546
maybe_cast_to_integer_array,
@@ -4360,6 +4361,8 @@ def _validate_fill_value(self, value):
43604361
TypeError
43614362
If the value cannot be inserted into an array of this dtype.
43624363
"""
4364+
if not can_hold_element(self.dtype, value):
4365+
raise TypeError
43634366
return value
43644367

43654368
@final

pandas/core/indexes/datetimelike.py

+6
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,12 @@ def insert(self, loc: int, item):
609609
result._data._freq = self._get_insert_freq(loc, item)
610610
return result
611611

612+
def _validate_fill_value(self, value):
613+
"""
614+
Convert value to be insertable to ndarray.
615+
"""
616+
return self._data._validate_setitem_value(value)
617+
612618
# --------------------------------------------------------------------
613619
# Join/Set Methods
614620

pandas/core/indexes/datetimes.py

-6
Original file line numberDiff line numberDiff line change
@@ -342,12 +342,6 @@ def __reduce__(self):
342342
d.update(self._get_attributes_dict())
343343
return _new_DatetimeIndex, (type(self), d), None
344344

345-
def _validate_fill_value(self, value):
346-
"""
347-
Convert value to be insertable to ndarray.
348-
"""
349-
return self._data._validate_setitem_value(value)
350-
351345
def _is_comparable_dtype(self, dtype: DtypeObj) -> bool:
352346
"""
353347
Can we compare values of the given dtype to our own?

pandas/core/indexes/numeric.py

-39
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@
1010
from pandas.core.dtypes.cast import astype_nansafe
1111
from pandas.core.dtypes.common import (
1212
is_bool,
13-
is_bool_dtype,
1413
is_dtype_equal,
1514
is_extension_array_dtype,
1615
is_float,
1716
is_float_dtype,
1817
is_integer_dtype,
19-
is_number,
2018
is_numeric_dtype,
2119
is_scalar,
2220
is_signed_integer_dtype,
@@ -25,7 +23,6 @@
2523
pandas_dtype,
2624
)
2725
from pandas.core.dtypes.generic import ABCSeries
28-
from pandas.core.dtypes.missing import is_valid_nat_for_dtype, isna
2926

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

125-
@doc(Index._validate_fill_value)
126-
def _validate_fill_value(self, value):
127-
if is_bool(value) or is_bool_dtype(value):
128-
# force conversion to object
129-
# so we don't lose the bools
130-
raise TypeError
131-
elif is_scalar(value) and isna(value):
132-
if is_valid_nat_for_dtype(value, self.dtype):
133-
value = self._na_value
134-
if self.dtype.kind != "f":
135-
# raise so that caller can cast
136-
raise TypeError
137-
else:
138-
# NaT, np.datetime64("NaT"), np.timedelta64("NaT")
139-
raise TypeError
140-
141-
elif is_scalar(value):
142-
if not is_number(value):
143-
# e.g. datetime64, timedelta64, datetime, ...
144-
raise TypeError
145-
146-
elif lib.is_complex(value):
147-
# at least until we have a ComplexIndx
148-
raise TypeError
149-
150-
elif is_float(value) and self.dtype.kind != "f":
151-
if not value.is_integer():
152-
raise TypeError
153-
value = int(value)
154-
155-
elif hasattr(value, "dtype") and value.dtype.kind in ["m", "M"]:
156-
# TODO: if we're checking arraylike here, do so systematically
157-
raise TypeError
158-
159-
return value
160-
161122
def _convert_tolerance(self, tolerance, target):
162123
tolerance = np.asarray(tolerance)
163124
if target.size != tolerance.size and tolerance.size > 1:

0 commit comments

Comments
 (0)