diff --git a/pandas/core/nanops.py b/pandas/core/nanops.py index fe88622a04bb4..eb442e8bf3486 100644 --- a/pandas/core/nanops.py +++ b/pandas/core/nanops.py @@ -97,17 +97,21 @@ def f(values, axis=None, skipna=True, **kwds): for k, v in self.kwargs.items(): if k not in kwds: kwds[k] = v - try: - if values.size == 0 and kwds.get("min_count") is None: - # We are empty, returning NA for our type - # Only applies for the default `min_count` of None - # since that affects how empty arrays are handled. - # TODO(GH-18976) update all the nanops methods to - # correctly handle empty inputs and remove this check. - # It *may* just be `var` - return _na_for_min_count(values, axis) - - if _USE_BOTTLENECK and skipna and _bn_ok_dtype(values.dtype, bn_name): + + if values.size == 0 and kwds.get("min_count") is None: + # We are empty, returning NA for our type + # Only applies for the default `min_count` of None + # since that affects how empty arrays are handled. + # TODO(GH-18976) update all the nanops methods to + # correctly handle empty inputs and remove this check. + # It *may* just be `var` + return _na_for_min_count(values, axis) + + if _USE_BOTTLENECK and skipna and _bn_ok_dtype(values.dtype, bn_name): + if kwds.get("mask", None) is None: + # `mask` is not recognised by bottleneck, would raise + # TypeError if called + kwds.pop("mask", None) result = bn_func(values, axis=axis, **kwds) # prefer to treat inf/-inf as NA, but must compute the func @@ -116,18 +120,8 @@ def f(values, axis=None, skipna=True, **kwds): result = alt(values, axis=axis, skipna=skipna, **kwds) else: result = alt(values, axis=axis, skipna=skipna, **kwds) - except Exception: - try: - result = alt(values, axis=axis, skipna=skipna, **kwds) - except ValueError as e: - # we want to transform an object array - # ValueError message to the more typical TypeError - # e.g. this is normally a disallowed function on - # object arrays that contain strings - - if is_object_dtype(values): - raise TypeError(e) - raise + else: + result = alt(values, axis=axis, skipna=skipna, **kwds) return result