@@ -442,12 +442,19 @@ def make_a_block(nv, ref_loc):
442
442
nv = _block_shape (nv , ndim = self .ndim )
443
443
except (AttributeError , NotImplementedError ):
444
444
pass
445
- block = self .make_block (values = nv ,
446
- placement = ref_loc , fastpath = True )
445
+
446
+ if isinstance (self , SparseBlock ):
447
+ block = self .make_block_same_class (values = nv ,
448
+ placement = ref_loc ,
449
+ fastpath = True )
450
+ else :
451
+ block = self .make_block (values = nv ,
452
+ placement = ref_loc ,
453
+ fastpath = True )
447
454
return block
448
455
449
456
# ndim == 1
450
- if self .ndim == 1 :
457
+ if self .ndim == 1 or isinstance ( self , SparseBlock ) :
451
458
if mask .any ():
452
459
nv = f (mask , new_values , None )
453
460
else :
@@ -527,7 +534,7 @@ def f(m, v, i):
527
534
528
535
return self .split_and_operate (None , f , False )
529
536
530
- def astype (self , dtype , copy = False , errors = 'raise' , values = None , ** kwargs ):
537
+ def astype (self , dtype , copy = True , errors = 'raise' , values = None , ** kwargs ):
531
538
return self ._astype (dtype , copy = copy , errors = errors , values = values ,
532
539
** kwargs )
533
540
@@ -613,7 +620,9 @@ def _can_hold_element(self, element):
613
620
element = np .asarray (element )
614
621
tipo = element .dtype .type
615
622
return issubclass (tipo , dtype )
616
- return isinstance (element , dtype )
623
+ else :
624
+ element_dtype = infer_dtype_from (element , pandas_dtype = True )[0 ]
625
+ return isinstance (element , dtype ) or dtype == element_dtype
617
626
618
627
def _try_cast_result (self , result , dtype = None ):
619
628
""" try to cast the result to our original type, we may have
@@ -1394,6 +1403,8 @@ def where(self, other, cond, align=True, raise_on_error=True,
1394
1403
if not hasattr (cond , 'shape' ):
1395
1404
raise ValueError ("where must have a condition that is ndarray "
1396
1405
"like" )
1406
+ else :
1407
+ cond = cond .reshape (values .shape )
1397
1408
1398
1409
# our where function
1399
1410
def func (cond , values , other ):
@@ -1440,7 +1451,10 @@ def func(cond, values, other):
1440
1451
if try_cast :
1441
1452
result = self ._try_cast_result (result )
1442
1453
1443
- return self .make_block (result )
1454
+ if isinstance (self , SparseBlock ):
1455
+ return self .make_block_same_class (result , self .mgr_locs )
1456
+ else :
1457
+ return self .make_block (result )
1444
1458
1445
1459
# might need to separate out blocks
1446
1460
axis = cond .ndim - 1
@@ -1454,7 +1468,8 @@ def func(cond, values, other):
1454
1468
r = self ._try_cast_result (result .take (m .nonzero ()[0 ],
1455
1469
axis = axis ))
1456
1470
result_blocks .append (
1457
- self .make_block (r .T , placement = self .mgr_locs [m ]))
1471
+ self .make_block_same_class (r .T ,
1472
+ placement = self .mgr_locs [m ]))
1458
1473
1459
1474
return result_blocks
1460
1475
@@ -2653,7 +2668,7 @@ def sp_index(self):
2653
2668
def kind (self ):
2654
2669
return self .values .kind
2655
2670
2656
- def _astype (self , dtype , copy = False , raise_on_error = True , values = None ,
2671
+ def _astype (self , dtype , copy = True , raise_on_error = True , values = None ,
2657
2672
klass = None , mgr = None , ** kwargs ):
2658
2673
if values is None :
2659
2674
values = self .values
0 commit comments