Skip to content

Commit 4f3399e

Browse files
authored
fix: out-of-bounds datetime.date raises OutOfBoundsDatetime (#180)
1 parent a7fc29a commit 4f3399e

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

db_dtypes/__init__.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -246,19 +246,18 @@ def _datetime(
246246
scalar,
247247
match_fn=re.compile(r"\s*(?P<year>\d+)-(?P<month>\d+)-(?P<day>\d+)\s*$").match,
248248
) -> Optional[numpy.datetime64]:
249-
if isinstance(scalar, numpy.datetime64):
250-
return scalar
251-
252249
# Convert pyarrow values to datetime.date.
253250
if isinstance(scalar, (pyarrow.Date32Scalar, pyarrow.Date64Scalar)):
254251
scalar = scalar.as_py()
255252

256253
if pandas.isna(scalar):
257254
return numpy.datetime64("NaT")
255+
elif isinstance(scalar, numpy.datetime64):
256+
dateObj = pandas.Timestamp(scalar)
258257
elif isinstance(scalar, datetime.date):
259-
return pandas.Timestamp(
258+
dateObj = pandas.Timestamp(
260259
year=scalar.year, month=scalar.month, day=scalar.day
261-
).to_datetime64()
260+
)
262261
elif isinstance(scalar, str):
263262
match = match_fn(scalar)
264263
if not match:
@@ -272,14 +271,16 @@ def _datetime(
272271
month=month,
273272
day=day,
274273
)
275-
if pandas.Timestamp.min < dateObj < pandas.Timestamp.max:
276-
return dateObj.to_datetime64()
277-
else: # pragma: NO COVER
278-
# TODO(#166): Include these lines in coverage when pandas 2.0 is released.
279-
raise OutOfBoundsDatetime("Out of bounds", scalar) # pragma: NO COVER
280274
else:
281275
raise TypeError("Invalid value type", scalar)
282276

277+
# TODO(#64): Support larger ranges with other units.
278+
if pandas.Timestamp.min < dateObj < pandas.Timestamp.max:
279+
return dateObj.to_datetime64()
280+
else: # pragma: NO COVER
281+
# TODO(#166): Include these lines in coverage when pandas 2.0 is released.
282+
raise OutOfBoundsDatetime("Out of bounds", scalar) # pragma: NO COVER
283+
283284
def _box_func(self, x):
284285
if pandas.isna(x):
285286
return pandas.NaT

tests/unit/test_date.py

+2
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ def test_date_parsing_errors(value, error):
159159
("9999-12-31", "Out of bounds"),
160160
("1677-09-21", "Out of bounds"),
161161
("2262-04-12", "Out of bounds"),
162+
(datetime.date(1, 1, 1), "Out of bounds"),
163+
(datetime.date(9999, 12, 31), "Out of bounds"),
162164
],
163165
)
164166
def test_date_parsing_errors_out_of_bounds(value, error):

0 commit comments

Comments
 (0)