Skip to content

Commit 32d7301

Browse files
committed
BUG: nanosecond out-of-bounds checking in array conversions. close #1475
1 parent 5fbffe4 commit 32d7301

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

pandas/src/datetime.pyx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,11 @@ cdef inline _check_dts_bounds(int64_t value, pandas_datetimestruct *dts):
560560
if dts.year <= 1677 or dts.year >= 2262:
561561
pandas_datetime_to_datetimestruct(value, PANDAS_FR_ns, &dts2)
562562
if dts2.year != dts.year:
563-
raise ValueError('Out of bounds timestamp in year: %s' % dts.year)
563+
fmt = '%d-%.2d-%.2d %.2d:%.2d:%.2d' % (dts.year, dts.month,
564+
dts.day, dts.hour,
565+
dts.min, dts.sec)
566+
567+
raise ValueError('Out of bounds nanosecond timestamp: %s' % fmt)
564568

565569
# elif isinstance(ts, _Timestamp):
566570
# tmp = ts
@@ -645,8 +649,10 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False):
645649
iresult[i] = iNaT
646650
elif PyDateTime_Check(val):
647651
iresult[i] = _pydatetime_to_dts(val, &dts)
652+
_check_dts_bounds(iresult[i], &dts)
648653
elif PyDate_Check(val):
649654
iresult[i] = _date_to_datetime64(val, &dts)
655+
_check_dts_bounds(iresult[i], &dts)
650656
elif util.is_datetime64_object(val):
651657
iresult[i] = _get_datetime64_nanos(val)
652658
elif util.is_integer_object(val):
@@ -659,6 +665,9 @@ def array_to_datetime(ndarray[object] values, raise_=False, dayfirst=False):
659665
result[i] = parse(val, dayfirst=dayfirst)
660666
except Exception:
661667
raise TypeError
668+
pandas_datetime_to_datetimestruct(iresult[i], PANDAS_FR_ns,
669+
&dts)
670+
_check_dts_bounds(iresult[i], &dts)
662671
return result
663672
except TypeError:
664673
oresult = np.empty(n, dtype=object)

pandas/tseries/tests/test_timeseries.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,10 @@ def test_timestamp_date_out_of_range(self):
902902
self.assertRaises(ValueError, Timestamp, '1676-01-01')
903903
self.assertRaises(ValueError, Timestamp, '2263-01-01')
904904

905+
# 1475
906+
self.assertRaises(ValueError, DatetimeIndex, ['1400-01-01'])
907+
self.assertRaises(ValueError, DatetimeIndex, [datetime(1400, 1, 1)])
908+
905909
def test_timestamp_repr(self):
906910
# pre-1900
907911
stamp = Timestamp('1850-01-01', tz='US/Eastern')

0 commit comments

Comments
 (0)