@@ -593,22 +593,21 @@ def _astype(self, dtype, copy=False, errors='raise', values=None,
593
593
values = self .get_values (dtype = dtype )
594
594
595
595
# _astype_nansafe works fine with 1-d only
596
- values = astype_nansafe (
597
- values . ravel () , dtype , copy = True , ** kwargs )
596
+ vals1d = values . ravel ()
597
+ values = astype_nansafe ( vals1d , dtype , copy = True , ** kwargs )
598
598
599
599
# TODO(extension)
600
600
# should we make this attribute?
601
- try :
601
+ if isinstance ( values , np . ndarray ) :
602
602
values = values .reshape (self .shape )
603
- except AttributeError :
604
- pass
605
603
606
- newb = make_block (values , placement = self .mgr_locs ,
607
- ndim = self .ndim )
608
604
except Exception : # noqa: E722
609
605
if errors == 'raise' :
610
606
raise
611
607
newb = self .copy () if copy else self
608
+ else :
609
+ newb = make_block (values , placement = self .mgr_locs ,
610
+ ndim = self .ndim )
612
611
613
612
if newb .is_numeric and self .is_numeric :
614
613
if newb .shape != self .shape :
@@ -1311,10 +1310,6 @@ def where(self, other, cond, align=True, errors='raise',
1311
1310
1312
1311
# our where function
1313
1312
def func (cond , values , other ):
1314
- if cond .ravel ().all ():
1315
- return values
1316
-
1317
- values = self ._coerce_values (values )
1318
1313
other = self ._try_coerce_args (other )
1319
1314
1320
1315
try :
@@ -1331,20 +1326,24 @@ def func(cond, values, other):
1331
1326
result .fill (np .nan )
1332
1327
return result
1333
1328
1334
- # see if we can operate on the entire block, or need item-by-item
1335
- # or if we are a single block (ndim == 1)
1336
- try :
1337
- result = func (cond , values , other )
1338
- except TypeError :
1339
-
1340
- # we cannot coerce, return a compat dtype
1341
- # we are explicitly ignoring errors
1342
- block = self .coerce_to_target_dtype (other )
1343
- blocks = block .where (orig_other , cond , align = align ,
1344
- errors = errors ,
1345
- try_cast = try_cast , axis = axis ,
1346
- transpose = transpose )
1347
- return self ._maybe_downcast (blocks , 'infer' )
1329
+ if cond .ravel ().all ():
1330
+ result = values
1331
+ else :
1332
+ # see if we can operate on the entire block, or need item-by-item
1333
+ # or if we are a single block (ndim == 1)
1334
+ values = self ._coerce_values (values )
1335
+ try :
1336
+ result = func (cond , values , other )
1337
+ except TypeError :
1338
+
1339
+ # we cannot coerce, return a compat dtype
1340
+ # we are explicitly ignoring errors
1341
+ block = self .coerce_to_target_dtype (other )
1342
+ blocks = block .where (orig_other , cond , align = align ,
1343
+ errors = errors ,
1344
+ try_cast = try_cast , axis = axis ,
1345
+ transpose = transpose )
1346
+ return self ._maybe_downcast (blocks , 'infer' )
1348
1347
1349
1348
if self ._can_hold_na or self .ndim == 1 :
1350
1349
@@ -1456,7 +1455,8 @@ def quantile(self, qs, interpolation='linear', axis=0):
1456
1455
len (qs ))
1457
1456
else :
1458
1457
# asarray needed for Sparse, see GH#24600
1459
- # TODO: Why self.values and not values?
1458
+ # Note: we use self.values below instead of values because the
1459
+ # `asi8` conversion above will behave differently under `isna`
1460
1460
mask = np .asarray (isna (self .values ))
1461
1461
result = nanpercentile (values , np .array (qs ) * 100 ,
1462
1462
axis = axis , na_value = self .fill_value ,
@@ -2652,10 +2652,9 @@ def convert(self, *args, **kwargs):
2652
2652
def f (m , v , i ):
2653
2653
shape = v .shape
2654
2654
values = fn (v .ravel (), ** fn_kwargs )
2655
- try :
2655
+ if isinstance (values , np .ndarray ):
2656
+ # TODO: allow EA once reshape is supported
2656
2657
values = values .reshape (shape )
2657
- except (AttributeError , NotImplementedError ):
2658
- pass
2659
2658
2660
2659
values = _block_shape (values , ndim = self .ndim )
2661
2660
return values
@@ -2669,26 +2668,6 @@ def f(m, v, i):
2669
2668
2670
2669
return blocks
2671
2670
2672
- def set (self , locs , values ):
2673
- """
2674
- Modify Block in-place with new item value
2675
-
2676
- Returns
2677
- -------
2678
- None
2679
- """
2680
- try :
2681
- self .values [locs ] = values
2682
- except (ValueError ):
2683
-
2684
- # broadcasting error
2685
- # see GH6171
2686
- new_shape = list (values .shape )
2687
- new_shape [0 ] = len (self .items )
2688
- self .values = np .empty (tuple (new_shape ), dtype = self .dtype )
2689
- self .values .fill (np .nan )
2690
- self .values [locs ] = values
2691
-
2692
2671
def _maybe_downcast (self , blocks , downcast = None ):
2693
2672
2694
2673
if downcast is not None :
0 commit comments