Skip to content

Commit 5ebb1e4

Browse files
jbrockmendeljreback
authored andcommitted
CLN: dont catch Exception when calling maybe_convert_numeric (#28513)
1 parent 773f341 commit 5ebb1e4

File tree

4 files changed

+54
-52
lines changed

4 files changed

+54
-52
lines changed

pandas/core/dtypes/cast.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ def maybe_convert_objects(values: np.ndarray, convert_numeric: bool = True):
796796
new_values = lib.maybe_convert_numeric(
797797
values, set(), coerce_numeric=True
798798
)
799-
except Exception:
799+
except (ValueError, TypeError):
800800
pass
801801
else:
802802
# if we are all nans then leave me alone
@@ -875,7 +875,7 @@ def soft_convert_objects(
875875
if numeric and is_object_dtype(values.dtype):
876876
try:
877877
converted = lib.maybe_convert_numeric(values, set(), coerce_numeric=True)
878-
except Exception:
878+
except (ValueError, TypeError):
879879
pass
880880
else:
881881
# If all NaNs, then do not-alter
@@ -953,9 +953,10 @@ def try_datetime(v):
953953
# we might have a sequence of the same-datetimes with tz's
954954
# if so coerce to a DatetimeIndex; if they are not the same,
955955
# then these stay as object dtype, xref GH19671
956+
from pandas._libs.tslibs import conversion
957+
from pandas import DatetimeIndex
958+
956959
try:
957-
from pandas._libs.tslibs import conversion
958-
from pandas import DatetimeIndex
959960

960961
values, tz = conversion.datetime_to_datetime64(v)
961962
return DatetimeIndex(values).tz_localize("UTC").tz_convert(tz=tz)

pandas/core/tools/numeric.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -137,21 +137,20 @@ def to_numeric(arg, errors="raise", downcast=None):
137137
else:
138138
values = arg
139139

140-
try:
141-
if is_numeric_dtype(values):
142-
pass
143-
elif is_datetime_or_timedelta_dtype(values):
144-
values = values.astype(np.int64)
145-
else:
146-
values = ensure_object(values)
147-
coerce_numeric = errors not in ("ignore", "raise")
140+
if is_numeric_dtype(values):
141+
pass
142+
elif is_datetime_or_timedelta_dtype(values):
143+
values = values.astype(np.int64)
144+
else:
145+
values = ensure_object(values)
146+
coerce_numeric = errors not in ("ignore", "raise")
147+
try:
148148
values = lib.maybe_convert_numeric(
149149
values, set(), coerce_numeric=coerce_numeric
150150
)
151-
152-
except Exception:
153-
if errors == "raise":
154-
raise
151+
except (ValueError, TypeError):
152+
if errors == "raise":
153+
raise
155154

156155
# attempt downcast only if the data has been successfully converted
157156
# to a numerical dtype and if a downcast method has been specified

pandas/io/parsers.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -1782,14 +1782,17 @@ def _infer_types(self, values, na_values, try_num_bool=True):
17821782
np.putmask(values, mask, np.nan)
17831783
return values, na_count
17841784

1785-
if try_num_bool:
1785+
if try_num_bool and is_object_dtype(values.dtype):
1786+
# exclude e.g DatetimeIndex here
17861787
try:
17871788
result = lib.maybe_convert_numeric(values, na_values, False)
1788-
na_count = isna(result).sum()
1789-
except Exception:
1789+
except (ValueError, TypeError):
1790+
# e.g. encountering datetime string gets ValueError
1791+
# TypeError can be raised in floatify
17901792
result = values
1791-
if values.dtype == np.object_:
1792-
na_count = parsers.sanitize_objects(result, na_values, False)
1793+
na_count = parsers.sanitize_objects(result, na_values, False)
1794+
else:
1795+
na_count = isna(result).sum()
17931796
else:
17941797
result = values
17951798
if values.dtype == np.object_:

pandas/tests/dtypes/test_inference.py

+30-31
Original file line numberDiff line numberDiff line change
@@ -379,45 +379,44 @@ def test_isinf_scalar(self):
379379
assert not libmissing.isneginf_scalar(1)
380380
assert not libmissing.isneginf_scalar("a")
381381

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

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

390393
msg = "Unable to parse string"
391394

392-
for infinity in infinities:
393-
for maybe_int in (True, False):
394-
out = lib.maybe_convert_numeric(
395-
np.array([infinity], dtype=object), na_values, maybe_int
396-
)
397-
tm.assert_numpy_array_equal(out, pos)
398-
399-
out = lib.maybe_convert_numeric(
400-
np.array(["-" + infinity], dtype=object), na_values, maybe_int
401-
)
402-
tm.assert_numpy_array_equal(out, neg)
403-
404-
out = lib.maybe_convert_numeric(
405-
np.array([infinity], dtype=object), na_values, maybe_int
406-
)
407-
tm.assert_numpy_array_equal(out, pos)
408-
409-
out = lib.maybe_convert_numeric(
410-
np.array(["+" + infinity], dtype=object), na_values, maybe_int
411-
)
412-
tm.assert_numpy_array_equal(out, pos)
413-
414-
# too many characters
415-
with pytest.raises(ValueError, match=msg):
416-
lib.maybe_convert_numeric(
417-
np.array(["foo_" + infinity], dtype=object),
418-
na_values,
419-
maybe_int,
420-
)
395+
out = lib.maybe_convert_numeric(
396+
np.array([infinity], dtype=object), na_values, maybe_int
397+
)
398+
tm.assert_numpy_array_equal(out, pos)
399+
400+
out = lib.maybe_convert_numeric(
401+
np.array(["-" + infinity], dtype=object), na_values, maybe_int
402+
)
403+
tm.assert_numpy_array_equal(out, neg)
404+
405+
out = lib.maybe_convert_numeric(
406+
np.array([infinity], dtype=object), na_values, maybe_int
407+
)
408+
tm.assert_numpy_array_equal(out, pos)
409+
410+
out = lib.maybe_convert_numeric(
411+
np.array(["+" + infinity], dtype=object), na_values, maybe_int
412+
)
413+
tm.assert_numpy_array_equal(out, pos)
414+
415+
# too many characters
416+
with pytest.raises(ValueError, match=msg):
417+
lib.maybe_convert_numeric(
418+
np.array(["foo_" + infinity], dtype=object), na_values, maybe_int
419+
)
421420

422421
def test_maybe_convert_numeric_post_floatify_nan(self, coerce):
423422
# see gh-13314

0 commit comments

Comments
 (0)