Skip to content

Commit df3d9b2

Browse files
jbrockmendeljreback
authored andcommitted
CLN: handle bare exceptions im timedeltas, timestamps, reduction (#28346)
1 parent 5c57e7b commit df3d9b2

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

pandas/_libs/reduction.pyx

+3-1
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,8 @@ def apply_frame_axis0(object frame, object f, object names,
528528

529529
try:
530530
piece = f(chunk)
531-
except:
531+
except Exception:
532+
# We can't be more specific without knowing something about `f`
532533
raise InvalidApply('Let this error raise above us')
533534

534535
# Need to infer if low level index slider will cause segfaults
@@ -539,6 +540,7 @@ def apply_frame_axis0(object frame, object f, object names,
539540
else:
540541
mutated = True
541542
except AttributeError:
543+
# `piece` might not have an index, could be e.g. an int
542544
pass
543545

544546
results.append(piece)

pandas/_libs/tslibs/c_timestamp.pyx

+2-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ cdef class _Timestamp(datetime):
140140

141141
try:
142142
stamp += zone.strftime(' %%Z')
143-
except:
143+
except AttributeError:
144+
# e.g. tzlocal has no `strftime`
144145
pass
145146

146147
tz = ", tz='{0}'".format(zone) if zone is not None else ""

pandas/_libs/tslibs/timedeltas.pyx

+8-3
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,13 @@ def array_to_timedelta64(object[:] values, unit='ns', errors='raise'):
228228
# this is where all of the error handling will take place.
229229
try:
230230
for i in range(n):
231-
result[i] = parse_timedelta_string(values[i])
232-
except:
231+
if values[i] is NaT:
232+
# we allow this check in the fast-path because NaT is a C-object
233+
# so this is an inexpensive check
234+
iresult[i] = NPY_NAT
235+
else:
236+
result[i] = parse_timedelta_string(values[i])
237+
except (TypeError, ValueError):
233238
unit = parse_timedelta_unit(unit)
234239
for i in range(n):
235240
try:
@@ -309,7 +314,7 @@ cdef inline int64_t cast_from_unit(object ts, object unit) except? -1:
309314
return <int64_t>(base * m) + <int64_t>(frac * m)
310315

311316

312-
cdef inline parse_timedelta_string(object ts):
317+
cdef inline int64_t parse_timedelta_string(str ts) except? -1:
313318
"""
314319
Parse a regular format timedelta string. Return an int64_t (in ns)
315320
or raise a ValueError on an invalid parse.

0 commit comments

Comments
 (0)