Skip to content

Commit 37b2268

Browse files
jbrockmendeljreback
authored andcommitted
CLN: more blocks code out from try/excepts (#27200)
1 parent c0a4964 commit 37b2268

File tree

2 files changed

+30
-51
lines changed

2 files changed

+30
-51
lines changed

pandas/core/internals/blocks.py

Lines changed: 28 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -593,22 +593,21 @@ def _astype(self, dtype, copy=False, errors='raise', values=None,
593593
values = self.get_values(dtype=dtype)
594594

595595
# _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)
598598

599599
# TODO(extension)
600600
# should we make this attribute?
601-
try:
601+
if isinstance(values, np.ndarray):
602602
values = values.reshape(self.shape)
603-
except AttributeError:
604-
pass
605603

606-
newb = make_block(values, placement=self.mgr_locs,
607-
ndim=self.ndim)
608604
except Exception: # noqa: E722
609605
if errors == 'raise':
610606
raise
611607
newb = self.copy() if copy else self
608+
else:
609+
newb = make_block(values, placement=self.mgr_locs,
610+
ndim=self.ndim)
612611

613612
if newb.is_numeric and self.is_numeric:
614613
if newb.shape != self.shape:
@@ -1311,10 +1310,6 @@ def where(self, other, cond, align=True, errors='raise',
13111310

13121311
# our where function
13131312
def func(cond, values, other):
1314-
if cond.ravel().all():
1315-
return values
1316-
1317-
values = self._coerce_values(values)
13181313
other = self._try_coerce_args(other)
13191314

13201315
try:
@@ -1331,20 +1326,24 @@ def func(cond, values, other):
13311326
result.fill(np.nan)
13321327
return result
13331328

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')
13481347

13491348
if self._can_hold_na or self.ndim == 1:
13501349

@@ -1456,7 +1455,8 @@ def quantile(self, qs, interpolation='linear', axis=0):
14561455
len(qs))
14571456
else:
14581457
# 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`
14601460
mask = np.asarray(isna(self.values))
14611461
result = nanpercentile(values, np.array(qs) * 100,
14621462
axis=axis, na_value=self.fill_value,
@@ -2652,10 +2652,9 @@ def convert(self, *args, **kwargs):
26522652
def f(m, v, i):
26532653
shape = v.shape
26542654
values = fn(v.ravel(), **fn_kwargs)
2655-
try:
2655+
if isinstance(values, np.ndarray):
2656+
# TODO: allow EA once reshape is supported
26562657
values = values.reshape(shape)
2657-
except (AttributeError, NotImplementedError):
2658-
pass
26592658

26602659
values = _block_shape(values, ndim=self.ndim)
26612660
return values
@@ -2669,26 +2668,6 @@ def f(m, v, i):
26692668

26702669
return blocks
26712670

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-
26922671
def _maybe_downcast(self, blocks, downcast=None):
26932672

26942673
if downcast is not None:

pandas/core/internals/managers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,7 +1214,7 @@ def _slice_take_blocks_ax0(self, slice_or_indexer, fill_tuple=None):
12141214
elif not allow_fill or self.ndim == 1:
12151215
if allow_fill and fill_tuple[0] is None:
12161216
_, fill_value = maybe_promote(blk.dtype)
1217-
fill_tuple = (fill_value, )
1217+
fill_tuple = (fill_value,)
12181218

12191219
return [blk.take_nd(slobj, axis=0,
12201220
new_mgr_locs=slice(0, sllen),
@@ -1579,7 +1579,7 @@ def create_block_manager_from_blocks(blocks, axes):
15791579
mgr._consolidate_inplace()
15801580
return mgr
15811581

1582-
except (ValueError) as e:
1582+
except ValueError as e:
15831583
blocks = [getattr(b, 'values', b) for b in blocks]
15841584
tot_items = sum(b.shape[0] for b in blocks)
15851585
construction_error(tot_items, blocks[0].shape[1:], axes, e)

0 commit comments

Comments
 (0)