Skip to content

Commit fe69cf2

Browse files
jbrockmendeljmg7173
authored andcommitted
CLN: Exception in dtypes.cast (pandas-dev#28420)
1 parent 442f3f2 commit fe69cf2

File tree

2 files changed

+39
-36
lines changed

2 files changed

+39
-36
lines changed

pandas/_libs/tslib.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ cpdef array_to_datetime(ndarray[object] values, str errors='raise',
613613
# to check if all arguments have the same tzinfo
614614
tz = py_dt.utcoffset()
615615

616-
except Exception:
616+
except (ValueError, OverflowError):
617617
if is_coerce:
618618
iresult[i] = NPY_NAT
619619
continue

pandas/core/dtypes/cast.py

+38-35
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,16 @@ def maybe_downcast_to_dtype(result, dtype):
134134
# a datetimelike
135135
# GH12821, iNaT is casted to float
136136
if dtype.kind in ["M", "m"] and result.dtype.kind in ["i", "f"]:
137-
try:
138-
result = result.astype(dtype)
139-
except Exception:
137+
if hasattr(dtype, "tz"):
138+
# not a numpy dtype
140139
if dtype.tz:
141140
# convert to datetime and change timezone
142141
from pandas import to_datetime
143142

144143
result = to_datetime(result).tz_localize("utc")
145144
result = result.tz_convert(dtype.tz)
145+
else:
146+
result = result.astype(dtype)
146147

147148
elif dtype.type is Period:
148149
# TODO(DatetimeArray): merge with previous elif
@@ -214,14 +215,13 @@ def trans(x):
214215
and notna(result).all()
215216
):
216217
new_result = trans(result).astype(dtype)
217-
try:
218-
if np.allclose(new_result, result, rtol=0):
219-
return new_result
220-
except Exception:
221-
# comparison of an object dtype with a number type could
222-
# hit here
218+
if new_result.dtype.kind == "O" or result.dtype.kind == "O":
219+
# np.allclose may raise TypeError on object-dtype
223220
if (new_result == result).all():
224221
return new_result
222+
else:
223+
if np.allclose(new_result, result, rtol=0):
224+
return new_result
225225

226226
elif (
227227
issubclass(dtype.type, np.floating)
@@ -286,14 +286,18 @@ def changeit():
286286
# length of the boolean
287287
try:
288288
om = other[mask]
289+
except (IndexError, TypeError):
290+
# IndexError occurs in test_upcast when we have a boolean
291+
# mask of the wrong shape
292+
# TypeError occurs in test_upcast when `other` is a bool
293+
pass
294+
else:
289295
om_at = om.astype(result.dtype)
290296
if (om == om_at).all():
291297
new_result = result.values.copy()
292298
new_result[mask] = om_at
293299
result[:] = new_result
294300
return result, False
295-
except Exception:
296-
pass
297301

298302
# we are forced to change the dtype of the result as the input
299303
# isn't compatible
@@ -324,7 +328,8 @@ def changeit():
324328

325329
try:
326330
np.place(result, mask, other)
327-
except Exception:
331+
except TypeError:
332+
# e.g. int-dtype result and float-dtype other
328333
return changeit()
329334

330335
return result, False
@@ -636,24 +641,21 @@ def coerce_to_dtypes(result, dtypes):
636641
raise AssertionError("_coerce_to_dtypes requires equal len arrays")
637642

638643
def conv(r, dtype):
639-
try:
640-
if isna(r):
641-
pass
642-
elif dtype == _NS_DTYPE:
643-
r = tslibs.Timestamp(r)
644-
elif dtype == _TD_DTYPE:
645-
r = tslibs.Timedelta(r)
646-
elif dtype == np.bool_:
647-
# messy. non 0/1 integers do not get converted.
648-
if is_integer(r) and r not in [0, 1]:
649-
return int(r)
650-
r = bool(r)
651-
elif dtype.kind == "f":
652-
r = float(r)
653-
elif dtype.kind == "i":
654-
r = int(r)
655-
except Exception:
644+
if np.any(isna(r)):
656645
pass
646+
elif dtype == _NS_DTYPE:
647+
r = tslibs.Timestamp(r)
648+
elif dtype == _TD_DTYPE:
649+
r = tslibs.Timedelta(r)
650+
elif dtype == np.bool_:
651+
# messy. non 0/1 integers do not get converted.
652+
if is_integer(r) and r not in [0, 1]:
653+
return int(r)
654+
r = bool(r)
655+
elif dtype.kind == "f":
656+
r = float(r)
657+
elif dtype.kind == "i":
658+
r = int(r)
657659

658660
return r
659661

@@ -794,13 +796,13 @@ def maybe_convert_objects(values: np.ndarray, convert_numeric: bool = True):
794796
new_values = lib.maybe_convert_numeric(
795797
values, set(), coerce_numeric=True
796798
)
797-
799+
except Exception:
800+
pass
801+
else:
798802
# if we are all nans then leave me alone
799803
if not isna(new_values).all():
800804
values = new_values
801805

802-
except Exception:
803-
pass
804806
else:
805807
# soft-conversion
806808
values = lib.maybe_convert_objects(values)
@@ -873,11 +875,12 @@ def soft_convert_objects(
873875
if numeric and is_object_dtype(values.dtype):
874876
try:
875877
converted = lib.maybe_convert_numeric(values, set(), coerce_numeric=True)
878+
except Exception:
879+
pass
880+
else:
876881
# If all NaNs, then do not-alter
877882
values = converted if not isna(converted).all() else values
878883
values = values.copy() if copy else values
879-
except Exception:
880-
pass
881884

882885
return values
883886

@@ -972,7 +975,7 @@ def try_timedelta(v):
972975

973976
try:
974977
return to_timedelta(v)._ndarray_values.reshape(shape)
975-
except Exception:
978+
except ValueError:
976979
return v.reshape(shape)
977980

978981
inferred_type = lib.infer_datetimelike_array(ensure_object(v))

0 commit comments

Comments
 (0)