@@ -413,12 +413,6 @@ def fillna(self, value, limit=None, inplace=False, downcast=None):
413
413
try :
414
414
# Note: we only call try_coerce_args to let it raise
415
415
self ._try_coerce_args (value )
416
-
417
- blocks = self .putmask (mask , value , inplace = inplace )
418
- blocks = [
419
- b .make_block (values = self ._try_coerce_result (b .values )) for b in blocks
420
- ]
421
- return self ._maybe_downcast (blocks , downcast )
422
416
except (TypeError , ValueError ):
423
417
424
418
# we can't process the value, but nothing to do
@@ -435,6 +429,12 @@ def f(m, v, i):
435
429
return block .fillna (value , limit = limit , inplace = inplace , downcast = None )
436
430
437
431
return self .split_and_operate (mask , f , inplace )
432
+ else :
433
+ blocks = self .putmask (mask , value , inplace = inplace )
434
+ blocks = [
435
+ b .make_block (values = self ._try_coerce_result (b .values )) for b in blocks
436
+ ]
437
+ return self ._maybe_downcast (blocks , downcast )
438
438
439
439
def split_and_operate (self , mask , f , inplace ):
440
440
"""
@@ -615,10 +615,9 @@ def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
615
615
return self .copy ()
616
616
return self
617
617
618
- try :
619
- # force the copy here
620
- if values is None :
621
-
618
+ if values is None :
619
+ try :
620
+ # force the copy here
622
621
if self .is_extension :
623
622
values = self .values .astype (dtype )
624
623
else :
@@ -644,10 +643,12 @@ def _astype(self, dtype, copy=False, errors="raise", values=None, **kwargs):
644
643
if isinstance (values , np .ndarray ):
645
644
values = values .reshape (self .shape )
646
645
647
- except Exception : # noqa: E722
648
- if errors == "raise" :
649
- raise
650
- newb = self .copy () if copy else self
646
+ except Exception : # noqa: E722
647
+ if errors == "raise" :
648
+ raise
649
+ newb = self .copy () if copy else self
650
+ else :
651
+ newb = make_block (values , placement = self .mgr_locs , ndim = self .ndim )
651
652
else :
652
653
newb = make_block (values , placement = self .mgr_locs , ndim = self .ndim )
653
654
@@ -861,13 +862,6 @@ def setitem(self, indexer, value):
861
862
values = self .values
862
863
try :
863
864
value = self ._try_coerce_args (value )
864
- values = self ._coerce_values (values )
865
- # can keep its own dtype
866
- if hasattr (value , "dtype" ) and is_dtype_equal (values .dtype , value .dtype ):
867
- dtype = self .dtype
868
- else :
869
- dtype = "infer"
870
-
871
865
except (TypeError , ValueError ):
872
866
# current dtype cannot store value, coerce to common dtype
873
867
find_dtype = False
@@ -891,6 +885,13 @@ def setitem(self, indexer, value):
891
885
if not is_dtype_equal (self .dtype , dtype ):
892
886
b = self .astype (dtype )
893
887
return b .setitem (indexer , value )
888
+ else :
889
+ values = self ._coerce_values (values )
890
+ # can keep its own dtype
891
+ if hasattr (value , "dtype" ) and is_dtype_equal (values .dtype , value .dtype ):
892
+ dtype = self .dtype
893
+ else :
894
+ dtype = "infer"
894
895
895
896
# value must be storeable at this moment
896
897
arr_value = np .array (value )
@@ -2041,13 +2042,14 @@ def where(
2041
2042
else :
2042
2043
dtype = self .dtype
2043
2044
2045
+ result = self .values .copy ()
2046
+ icond = ~ cond
2047
+ if lib .is_scalar (other ):
2048
+ set_other = other
2049
+ else :
2050
+ set_other = other [icond ]
2044
2051
try :
2045
- result = self .values .copy ()
2046
- icond = ~ cond
2047
- if lib .is_scalar (other ):
2048
- result [icond ] = other
2049
- else :
2050
- result [icond ] = other [icond ]
2052
+ result [icond ] = set_other
2051
2053
except (NotImplementedError , TypeError ):
2052
2054
# NotImplementedError for class not implementing `__setitem__`
2053
2055
# TypeError for SparseArray, which implements just to raise
@@ -2314,10 +2316,7 @@ def _try_coerce_args(self, other):
2314
2316
-------
2315
2317
base-type other
2316
2318
"""
2317
-
2318
- if isinstance (other , bool ):
2319
- raise TypeError
2320
- elif is_null_datetimelike (other ):
2319
+ if is_null_datetimelike (other ):
2321
2320
other = tslibs .iNaT
2322
2321
elif isinstance (other , (datetime , np .datetime64 , date )):
2323
2322
other = self ._box_func (other )
@@ -2689,9 +2688,7 @@ def _try_coerce_args(self, other):
2689
2688
base-type other
2690
2689
"""
2691
2690
2692
- if isinstance (other , bool ):
2693
- raise TypeError
2694
- elif is_null_datetimelike (other ):
2691
+ if is_null_datetimelike (other ):
2695
2692
other = tslibs .iNaT
2696
2693
elif isinstance (other , (timedelta , np .timedelta64 )):
2697
2694
other = Timedelta (other ).value
0 commit comments