@@ -988,7 +988,7 @@ def f(m, v, i):
988988
989989 return [self .make_block (new_values , fastpath = True )]
990990
991- def coerce_to_target_dtype (self , other ):
991+ def coerce_to_target_dtype (self , other , copy = False ):
992992 """
993993 coerce the current block to a dtype compat for other
994994 we will return a block, possibly object, and not raise
@@ -1005,7 +1005,7 @@ def coerce_to_target_dtype(self, other):
10051005
10061006 if self .is_bool or is_object_dtype (dtype ) or is_bool_dtype (dtype ):
10071007 # we don't upcast to bool
1008- return self .astype (object )
1008+ return self .astype (object , copy = copy )
10091009
10101010 elif ((self .is_float or self .is_complex ) and
10111011 (is_integer_dtype (dtype ) or is_float_dtype (dtype ))):
@@ -1019,14 +1019,14 @@ def coerce_to_target_dtype(self, other):
10191019 # not a datetime
10201020 if not ((is_datetime64_dtype (dtype ) or
10211021 is_datetime64tz_dtype (dtype )) and self .is_datetime ):
1022- return self .astype (object )
1022+ return self .astype (object , copy = copy )
10231023
10241024 # don't upcast timezone with different timezone or no timezone
10251025 mytz = getattr (self .dtype , 'tz' , None )
10261026 othertz = getattr (dtype , 'tz' , None )
10271027
10281028 if str (mytz ) != str (othertz ):
1029- return self .astype (object )
1029+ return self .astype (object , copy = copy )
10301030
10311031 raise AssertionError ("possible recursion in "
10321032 "coerce_to_target_dtype: {} {}" .format (
@@ -1036,18 +1036,18 @@ def coerce_to_target_dtype(self, other):
10361036
10371037 # not a timedelta
10381038 if not (is_timedelta64_dtype (dtype ) and self .is_timedelta ):
1039- return self .astype (object )
1039+ return self .astype (object , copy = copy )
10401040
10411041 raise AssertionError ("possible recursion in "
10421042 "coerce_to_target_dtype: {} {}" .format (
10431043 self , other ))
10441044
10451045 try :
1046- return self .astype (dtype )
1046+ return self .astype (dtype , copy = copy )
10471047 except (ValueError , TypeError ):
10481048 pass
10491049
1050- return self .astype (object )
1050+ return self .astype (object , copy = copy )
10511051
10521052 def interpolate (self , method = 'pad' , axis = 0 , index = None , values = None ,
10531053 inplace = False , limit = None , limit_direction = 'forward' ,
@@ -2636,6 +2636,9 @@ class SparseBlock(NonConsolidatableMixIn, Block):
26362636 _ftype = 'sparse'
26372637 _holder = SparseArray
26382638
2639+ def coerce_to_target_dtype (self , other , copy = True ):
2640+ return super (SparseBlock , self ).coerce_to_target_dtype (other , copy )
2641+
26392642 @property
26402643 def shape (self ):
26412644 return (len (self .mgr_locs ), self .sp_index .length )
0 commit comments