@@ -350,6 +350,7 @@ def maybe_cast_to_extension_array(cls: Type["ExtensionArray"], obj, dtype=None):
350350def maybe_upcast_putmask (result : np .ndarray , mask : np .ndarray , other ):
351351 """
352352 A safe version of putmask that potentially upcasts the result.
353+
353354 The result is replaced with the first N elements of other,
354355 where N is the number of True values in mask.
355356 If the length of other is shorter than N, other will be repeated.
@@ -399,24 +400,6 @@ def maybe_upcast_putmask(result: np.ndarray, mask: np.ndarray, other):
399400 other = np .array (other , dtype = result .dtype )
400401
401402 def changeit ():
402-
403- # try to directly set by expanding our array to full
404- # length of the boolean
405- try :
406- om = other [mask ]
407- except (IndexError , TypeError ):
408- # IndexError occurs in test_upcast when we have a boolean
409- # mask of the wrong shape
410- # TypeError occurs in test_upcast when `other` is a bool
411- pass
412- else :
413- om_at = om .astype (result .dtype )
414- if (om == om_at ).all ():
415- new_result = result .values .copy ()
416- new_result [mask ] = om_at
417- result [:] = new_result
418- return result , False
419-
420403 # we are forced to change the dtype of the result as the input
421404 # isn't compatible
422405 r , _ = maybe_upcast (result , fill_value = other , copy = True )
@@ -434,15 +417,8 @@ def changeit():
434417
435418 # we have a scalar or len 0 ndarray
436419 # and its nan and we are changing some values
437- if is_scalar (other ) or (isinstance (other , np .ndarray ) and other .ndim < 1 ):
438- if isna (other ):
439- return changeit ()
440-
441- # we have an ndarray and the masking has nans in it
442- else :
443-
444- if isna (other ).any ():
445- return changeit ()
420+ if isna (other ):
421+ return changeit ()
446422
447423 try :
448424 np .place (result , mask , other )
0 commit comments