Skip to content

Commit 9d77ffe

Browse files
committed
Add try/except to address inconsistent behavior with invalid dates. issue #10154
1 parent 9fef291 commit 9d77ffe

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

pandas/tslib.pyx

+17-11
Original file line numberDiff line numberDiff line change
@@ -2760,17 +2760,23 @@ def array_strptime(ndarray[object] values, object fmt, bint exact=True, bint coe
27602760
# Cannot pre-calculate datetime_date() since can change in Julian
27612761
# calculation and thus could have different value for the day of the wk
27622762
# calculation.
2763-
if julian == -1:
2764-
# Need to add 1 to result since first day of the year is 1, not 0.
2765-
julian = datetime_date(year, month, day).toordinal() - \
2766-
datetime_date(year, 1, 1).toordinal() + 1
2767-
else: # Assume that if they bothered to include Julian day it will
2768-
# be accurate.
2769-
datetime_result = datetime_date.fromordinal(
2770-
(julian - 1) + datetime_date(year, 1, 1).toordinal())
2771-
year = datetime_result.year
2772-
month = datetime_result.month
2773-
day = datetime_result.day
2763+
try:
2764+
if julian == -1:
2765+
# Need to add 1 to result since first day of the year is 1, not 0.
2766+
julian = datetime_date(year, month, day).toordinal() - \
2767+
datetime_date(year, 1, 1).toordinal() + 1
2768+
else: # Assume that if they bothered to include Julian day it will
2769+
# be accurate.
2770+
datetime_result = datetime_date.fromordinal(
2771+
(julian - 1) + datetime_date(year, 1, 1).toordinal())
2772+
year = datetime_result.year
2773+
month = datetime_result.month
2774+
day = datetime_result.day
2775+
except ValueError:
2776+
if coerce:
2777+
iresult[i] = iNaT
2778+
continue
2779+
raise
27742780
if weekday == -1:
27752781
weekday = datetime_date(year, month, day).weekday()
27762782

0 commit comments

Comments
 (0)