From 4f068146f8208d9489537d5260a3d7d8fe16343d Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 8 Jul 2019 12:00:22 -0700 Subject: [PATCH 1/2] catch less in try/except blocks --- pandas/core/internals/blocks.py | 60 +++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index bf6ebf1abe760..ab27d01eff3db 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -3376,36 +3376,44 @@ def _putmask_smart(v, m, n): # will work in the current dtype try: nn = n[m] - + except TypeError: + # TypeError: only integer scalar arrays can be converted to a scalar index + pass + else: # make sure that we have a nullable type # if we have nulls if not _isna_compat(v, nn[0]): - raise ValueError - - # we ignore ComplexWarning here - with warnings.catch_warnings(record=True): - warnings.simplefilter("ignore", np.ComplexWarning) - nn_at = nn.astype(v.dtype) - - # avoid invalid dtype comparisons - # between numbers & strings - - # only compare integers/floats - # don't compare integers to datetimelikes - if not is_numeric_v_string_like(nn, nn_at) and ( - is_float_dtype(nn.dtype) - or is_integer_dtype(nn.dtype) - and is_float_dtype(nn_at.dtype) - or is_integer_dtype(nn_at.dtype) - ): + pass + else: - comp = nn == nn_at - if is_list_like(comp) and comp.all(): - nv = v.copy() - nv[m] = nn_at - return nv - except (ValueError, IndexError, TypeError, OverflowError): - pass + try: + # we ignore ComplexWarning here + with warnings.catch_warnings(record=True): + warnings.simplefilter("ignore", np.ComplexWarning) + nn_at = nn.astype(v.dtype) + except (ValueError, TypeError): + # ValueError: could not convert string to float: 'foo' + # TypeError: int() argument must be a string, a bytes-like + # object or a number, not 'function' + pass + else: + # avoid invalid dtype comparisons + # between numbers & strings + + # only compare integers/floats + # don't compare integers to datetimelikes + if not is_numeric_v_string_like(nn, nn_at) and ( + is_float_dtype(nn.dtype) + or is_integer_dtype(nn.dtype) + and is_float_dtype(nn_at.dtype) + or is_integer_dtype(nn_at.dtype) + ): + + comp = nn == nn_at + if is_list_like(comp) and comp.all(): + nv = v.copy() + nv[m] = nn_at + return nv n = np.asarray(n) From f1ad105b740e4002238378ef7d329c468dc47f68 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 8 Jul 2019 14:04:45 -0700 Subject: [PATCH 2/2] do checks instead of try/except --- pandas/core/internals/blocks.py | 46 ++++++++++++++------------------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index ab27d01eff3db..2d936fd8290f1 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -3384,36 +3384,28 @@ def _putmask_smart(v, m, n): # if we have nulls if not _isna_compat(v, nn[0]): pass + elif is_numeric_v_string_like(nn, v): + # avoid invalid dtype comparisons + # between numbers & strings + pass + elif not (is_float_dtype(nn.dtype) or is_integer_dtype(nn.dtype)): + # only compare integers/floats + pass + elif not (is_float_dtype(v.dtype) or is_integer_dtype(v.dtype)): + # only compare integers/floats + pass else: - try: - # we ignore ComplexWarning here - with warnings.catch_warnings(record=True): - warnings.simplefilter("ignore", np.ComplexWarning) - nn_at = nn.astype(v.dtype) - except (ValueError, TypeError): - # ValueError: could not convert string to float: 'foo' - # TypeError: int() argument must be a string, a bytes-like - # object or a number, not 'function' - pass - else: - # avoid invalid dtype comparisons - # between numbers & strings - - # only compare integers/floats - # don't compare integers to datetimelikes - if not is_numeric_v_string_like(nn, nn_at) and ( - is_float_dtype(nn.dtype) - or is_integer_dtype(nn.dtype) - and is_float_dtype(nn_at.dtype) - or is_integer_dtype(nn_at.dtype) - ): + # we ignore ComplexWarning here + with warnings.catch_warnings(record=True): + warnings.simplefilter("ignore", np.ComplexWarning) + nn_at = nn.astype(v.dtype) - comp = nn == nn_at - if is_list_like(comp) and comp.all(): - nv = v.copy() - nv[m] = nn_at - return nv + comp = nn == nn_at + if is_list_like(comp) and comp.all(): + nv = v.copy() + nv[m] = nn_at + return nv n = np.asarray(n)