Skip to content

CLN: dont catch Exception when calling maybe_convert_numeric #28513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ def maybe_convert_objects(values: np.ndarray, convert_numeric: bool = True):
new_values = lib.maybe_convert_numeric(
values, set(), coerce_numeric=True
)
except Exception:
except (ValueError, TypeError):
pass
else:
# if we are all nans then leave me alone
Expand Down Expand Up @@ -875,7 +875,7 @@ def soft_convert_objects(
if numeric and is_object_dtype(values.dtype):
try:
converted = lib.maybe_convert_numeric(values, set(), coerce_numeric=True)
except Exception:
except (ValueError, TypeError):
pass
else:
# If all NaNs, then do not-alter
Expand Down Expand Up @@ -953,9 +953,10 @@ def try_datetime(v):
# we might have a sequence of the same-datetimes with tz's
# if so coerce to a DatetimeIndex; if they are not the same,
# then these stay as object dtype, xref GH19671
from pandas._libs.tslibs import conversion
from pandas import DatetimeIndex

try:
from pandas._libs.tslibs import conversion
from pandas import DatetimeIndex

values, tz = conversion.datetime_to_datetime64(v)
return DatetimeIndex(values).tz_localize("UTC").tz_convert(tz=tz)
Expand Down
23 changes: 11 additions & 12 deletions pandas/core/tools/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,20 @@ def to_numeric(arg, errors="raise", downcast=None):
else:
values = arg

try:
if is_numeric_dtype(values):
pass
elif is_datetime_or_timedelta_dtype(values):
values = values.astype(np.int64)
else:
values = ensure_object(values)
coerce_numeric = errors not in ("ignore", "raise")
if is_numeric_dtype(values):
pass
elif is_datetime_or_timedelta_dtype(values):
values = values.astype(np.int64)
else:
values = ensure_object(values)
coerce_numeric = errors not in ("ignore", "raise")
try:
values = lib.maybe_convert_numeric(
values, set(), coerce_numeric=coerce_numeric
)

except Exception:
if errors == "raise":
raise
except (ValueError, TypeError):
if errors == "raise":
raise

# attempt downcast only if the data has been successfully converted
# to a numerical dtype and if a downcast method has been specified
Expand Down
13 changes: 8 additions & 5 deletions pandas/io/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1782,14 +1782,17 @@ def _infer_types(self, values, na_values, try_num_bool=True):
np.putmask(values, mask, np.nan)
return values, na_count

if try_num_bool:
if try_num_bool and is_object_dtype(values.dtype):
# exclude e.g DatetimeIndex here
try:
result = lib.maybe_convert_numeric(values, na_values, False)
na_count = isna(result).sum()
except Exception:
except (ValueError, TypeError):
# e.g. encountering datetime string gets ValueError
# TypeError can be raised in floatify
result = values
if values.dtype == np.object_:
na_count = parsers.sanitize_objects(result, na_values, False)
na_count = parsers.sanitize_objects(result, na_values, False)
else:
na_count = isna(result).sum()
else:
result = values
if values.dtype == np.object_:
Expand Down
61 changes: 30 additions & 31 deletions pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,45 +379,44 @@ def test_isinf_scalar(self):
assert not libmissing.isneginf_scalar(1)
assert not libmissing.isneginf_scalar("a")

def test_maybe_convert_numeric_infinities(self):
@pytest.mark.parametrize("maybe_int", [True, False])
@pytest.mark.parametrize(
"infinity", ["inf", "inF", "iNf", "Inf", "iNF", "InF", "INf", "INF"]
)
def test_maybe_convert_numeric_infinities(self, infinity, maybe_int):
# see gh-13274
infinities = ["inf", "inF", "iNf", "Inf", "iNF", "InF", "INf", "INF"]
na_values = {"", "NULL", "nan"}

pos = np.array(["inf"], dtype=np.float64)
neg = np.array(["-inf"], dtype=np.float64)

msg = "Unable to parse string"

for infinity in infinities:
for maybe_int in (True, False):
out = lib.maybe_convert_numeric(
np.array([infinity], dtype=object), na_values, maybe_int
)
tm.assert_numpy_array_equal(out, pos)

out = lib.maybe_convert_numeric(
np.array(["-" + infinity], dtype=object), na_values, maybe_int
)
tm.assert_numpy_array_equal(out, neg)

out = lib.maybe_convert_numeric(
np.array([infinity], dtype=object), na_values, maybe_int
)
tm.assert_numpy_array_equal(out, pos)

out = lib.maybe_convert_numeric(
np.array(["+" + infinity], dtype=object), na_values, maybe_int
)
tm.assert_numpy_array_equal(out, pos)

# too many characters
with pytest.raises(ValueError, match=msg):
lib.maybe_convert_numeric(
np.array(["foo_" + infinity], dtype=object),
na_values,
maybe_int,
)
out = lib.maybe_convert_numeric(
np.array([infinity], dtype=object), na_values, maybe_int
)
tm.assert_numpy_array_equal(out, pos)

out = lib.maybe_convert_numeric(
np.array(["-" + infinity], dtype=object), na_values, maybe_int
)
tm.assert_numpy_array_equal(out, neg)

out = lib.maybe_convert_numeric(
np.array([infinity], dtype=object), na_values, maybe_int
)
tm.assert_numpy_array_equal(out, pos)

out = lib.maybe_convert_numeric(
np.array(["+" + infinity], dtype=object), na_values, maybe_int
)
tm.assert_numpy_array_equal(out, pos)

# too many characters
with pytest.raises(ValueError, match=msg):
lib.maybe_convert_numeric(
np.array(["foo_" + infinity], dtype=object), na_values, maybe_int
)

def test_maybe_convert_numeric_post_floatify_nan(self, coerce):
# see gh-13314
Expand Down