46
46
find_common_type )
47
47
from pandas .core .dtypes .missing import (
48
48
isna , notna , array_equivalent ,
49
+ na_value_for_dtype ,
49
50
_isna_compat ,
50
51
is_null_datelike_scalar )
51
52
import pandas .core .dtypes .concat as _concat
@@ -442,12 +443,19 @@ def make_a_block(nv, ref_loc):
442
443
nv = _block_shape (nv , ndim = self .ndim )
443
444
except (AttributeError , NotImplementedError ):
444
445
pass
445
- block = self .make_block (values = nv ,
446
- placement = ref_loc , fastpath = True )
446
+
447
+ if isinstance (self , SparseBlock ):
448
+ block = self .make_block_same_class (values = nv ,
449
+ placement = ref_loc ,
450
+ fastpath = True )
451
+ else :
452
+ block = self .make_block (values = nv ,
453
+ placement = ref_loc ,
454
+ fastpath = True )
447
455
return block
448
456
449
457
# ndim == 1
450
- if self .ndim == 1 :
458
+ if self .ndim == 1 or isinstance ( self , SparseBlock ) :
451
459
if mask .any ():
452
460
nv = f (mask , new_values , None )
453
461
else :
@@ -527,7 +535,7 @@ def f(m, v, i):
527
535
528
536
return self .split_and_operate (None , f , False )
529
537
530
- def astype (self , dtype , copy = False , errors = 'raise' , values = None , ** kwargs ):
538
+ def astype (self , dtype , copy = True , errors = 'raise' , values = None , ** kwargs ):
531
539
return self ._astype (dtype , copy = copy , errors = errors , values = values ,
532
540
** kwargs )
533
541
@@ -613,7 +621,9 @@ def _can_hold_element(self, element):
613
621
element = np .asarray (element )
614
622
tipo = element .dtype .type
615
623
return issubclass (tipo , dtype )
616
- return isinstance (element , dtype )
624
+ else :
625
+ element_dtype = infer_dtype_from (element , pandas_dtype = True )[0 ]
626
+ return isinstance (element , dtype ) or dtype == element_dtype
617
627
618
628
def _try_cast_result (self , result , dtype = None ):
619
629
""" try to cast the result to our original type, we may have
@@ -1394,6 +1404,8 @@ def where(self, other, cond, align=True, raise_on_error=True,
1394
1404
if not hasattr (cond , 'shape' ):
1395
1405
raise ValueError ("where must have a condition that is ndarray "
1396
1406
"like" )
1407
+ else :
1408
+ cond = cond .reshape (values .shape )
1397
1409
1398
1410
# our where function
1399
1411
def func (cond , values , other ):
@@ -1440,7 +1452,13 @@ def func(cond, values, other):
1440
1452
if try_cast :
1441
1453
result = self ._try_cast_result (result )
1442
1454
1443
- return self .make_block (result )
1455
+ if isinstance (self , SparseBlock ):
1456
+ fill_value = na_value_for_dtype (result .dtype )
1457
+ return self .make_block_same_class (result ,
1458
+ self .mgr_locs ,
1459
+ fill_value = fill_value )
1460
+ else :
1461
+ return self .make_block (result )
1444
1462
1445
1463
# might need to separate out blocks
1446
1464
axis = cond .ndim - 1
@@ -1454,7 +1472,8 @@ def func(cond, values, other):
1454
1472
r = self ._try_cast_result (result .take (m .nonzero ()[0 ],
1455
1473
axis = axis ))
1456
1474
result_blocks .append (
1457
- self .make_block (r .T , placement = self .mgr_locs [m ]))
1475
+ self .make_block_same_class (r .T ,
1476
+ placement = self .mgr_locs [m ]))
1458
1477
1459
1478
return result_blocks
1460
1479
@@ -2653,7 +2672,7 @@ def sp_index(self):
2653
2672
def kind (self ):
2654
2673
return self .values .kind
2655
2674
2656
- def _astype (self , dtype , copy = False , raise_on_error = True , values = None ,
2675
+ def _astype (self , dtype , copy = True , raise_on_error = True , values = None ,
2657
2676
klass = None , mgr = None , ** kwargs ):
2658
2677
if values is None :
2659
2678
values = self .values
0 commit comments