File tree 3 files changed +24
-23
lines changed
3 files changed +24
-23
lines changed Original file line number Diff line number Diff line change @@ -2239,11 +2239,26 @@ def np_can_hold_element(dtype: np.dtype, element: Any) -> Any:
2239
2239
casted = element .astype (dtype )
2240
2240
comp = casted == element
2241
2241
if comp .all ():
2242
- return element
2242
+ # Return the casted values bc they can be passed to
2243
+ # np.putmask, whereas the raw values cannot.
2244
+ # see TestSetitemFloatNDarrayIntoIntegerSeries
2245
+ return casted
2243
2246
raise ValueError
2244
2247
2245
2248
# Anything other than integer we cannot hold
2246
2249
raise ValueError
2250
+ elif (
2251
+ dtype .kind == "u"
2252
+ and isinstance (element , np .ndarray )
2253
+ and element .dtype .kind == "i"
2254
+ ):
2255
+ # see test_where_uint64
2256
+ casted = element .astype (dtype )
2257
+ if (casted == element ).all ():
2258
+ # TODO: faster to check (element >=0).all()? potential
2259
+ # itemsize issues there?
2260
+ return casted
2261
+ raise ValueError
2247
2262
elif dtype .itemsize < tipo .itemsize :
2248
2263
if is_integer (element ):
2249
2264
# e.g. test_setitem_series_int8 if we have a python int 1
Original file line number Diff line number Diff line change 69
69
find_common_type ,
70
70
infer_dtype_from ,
71
71
maybe_cast_pointwise_result ,
72
+ np_can_hold_element ,
72
73
)
73
74
from pandas .core .dtypes .common import (
74
75
ensure_int64 ,
@@ -4915,6 +4916,13 @@ def _validate_fill_value(self, value):
4915
4916
TypeError
4916
4917
If the value cannot be inserted into an array of this dtype.
4917
4918
"""
4919
+ dtype = self .dtype
4920
+ if isinstance (dtype , np .dtype ) and dtype .kind not in ["m" , "M" ]:
4921
+ try :
4922
+ return np_can_hold_element (dtype , value )
4923
+ except ValueError as err :
4924
+ # re-raise as TypeError for consistency
4925
+ raise TypeError from err
4918
4926
if not can_hold_element (self ._values , value ):
4919
4927
raise TypeError
4920
4928
return value
Original file line number Diff line number Diff line change @@ -419,18 +419,6 @@ def asi8(self) -> npt.NDArray[np.int64]:
419
419
)
420
420
return self ._values .view (self ._default_dtype )
421
421
422
- def _validate_fill_value (self , value ):
423
- # e.g. np.array([1.0]) we want np.array([1], dtype=self.dtype)
424
- # see TestSetitemFloatNDarrayIntoIntegerSeries
425
- super ()._validate_fill_value (value )
426
- if hasattr (value , "dtype" ) and is_float_dtype (value .dtype ):
427
- converted = value .astype (self .dtype )
428
- if (converted == value ).all ():
429
- # See also: can_hold_element
430
- return converted
431
- raise TypeError
432
- return value
433
-
434
422
435
423
class Int64Index (IntegerIndex ):
436
424
_index_descr_args = {
@@ -461,16 +449,6 @@ class UInt64Index(IntegerIndex):
461
449
_default_dtype = np .dtype (np .uint64 )
462
450
_dtype_validation_metadata = (is_unsigned_integer_dtype , "unsigned integer" )
463
451
464
- def _validate_fill_value (self , value ):
465
- # e.g. np.array([1]) we want np.array([1], dtype=np.uint64)
466
- # see test_where_uin64
467
- super ()._validate_fill_value (value )
468
- if hasattr (value , "dtype" ) and is_signed_integer_dtype (value .dtype ):
469
- if (value >= 0 ).all ():
470
- return value .astype (self .dtype )
471
- raise TypeError
472
- return value
473
-
474
452
475
453
class Float64Index (NumericIndex ):
476
454
_index_descr_args = {
You can’t perform that action at this time.
0 commit comments