Skip to content

Commit 357a315

Browse files
chalmerlowetswast
andauthored
fix: adds bounds checking because pandas now handles microsecond reso… (#166)
* fix: adds bounds checking because pandas now handles microsecond resolution * Update db_dtypes/__init__.py Co-authored-by: Tim Swast <[email protected]> Co-authored-by: Tim Swast <[email protected]>
1 parent ce4a560 commit 357a315

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

db_dtypes/__init__.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
import packaging.version
2424
import pandas
2525
import pandas.api.extensions
26+
from pandas.errors import OutOfBoundsDatetime
2627
import pyarrow
2728
import pyarrow.compute
2829

30+
2931
from db_dtypes.version import __version__
3032
from db_dtypes import core
3133

@@ -143,6 +145,7 @@ def _datetime(
143145
second = parsed.group("seconds")
144146
fraction = parsed.group("fraction")
145147
nanosecond = int(fraction.ljust(9, "0")[:9]) if fraction else 0
148+
146149
return pandas.Timestamp(
147150
year=1970,
148151
month=1,
@@ -263,7 +266,17 @@ def _datetime(
263266
year = int(match.group("year"))
264267
month = int(match.group("month"))
265268
day = int(match.group("day"))
266-
return pandas.Timestamp(year=year, month=month, day=day).to_datetime64()
269+
270+
dateObj = pandas.Timestamp(
271+
year=year,
272+
month=month,
273+
day=day,
274+
)
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
267280
else:
268281
raise TypeError("Invalid value type", scalar)
269282

tests/unit/test_date.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import numpy
1919
import numpy.testing
2020
import pandas
21+
from pandas.errors import OutOfBoundsDatetime
2122
import pandas.testing
2223
import pytest
2324

@@ -143,15 +144,25 @@ def test_date_set_slice_null():
143144
("2021-2-99", "day is out of range for month"),
144145
("2021-99-1", "month must be in 1[.][.]12"),
145146
("10000-1-1", "year 10000 is out of range"),
146-
# Outside of min/max values pandas.Timestamp.
147+
],
148+
)
149+
def test_date_parsing_errors(value, error):
150+
with pytest.raises(ValueError, match=error):
151+
pandas.Series([value], dtype="dbdate")
152+
153+
154+
@pytest.mark.parametrize(
155+
"value, error",
156+
[
157+
# Values that are outside of the min/max values allowed by pandas.Timestamp
147158
("0001-01-01", "Out of bounds"),
148159
("9999-12-31", "Out of bounds"),
149160
("1677-09-21", "Out of bounds"),
150161
("2262-04-12", "Out of bounds"),
151162
],
152163
)
153-
def test_date_parsing_errors(value, error):
154-
with pytest.raises(ValueError, match=error):
164+
def test_date_parsing_errors_out_of_bounds(value, error):
165+
with pytest.raises(OutOfBoundsDatetime, match=error):
155166
pandas.Series([value], dtype="dbdate")
156167

157168

0 commit comments

Comments
 (0)