Skip to content

Commit

Permalink
fix: adds bounds checking because pandas now handles microsecond reso… (
Browse files Browse the repository at this point in the history
#166)

* fix: adds bounds checking because pandas now handles microsecond resolution

* Update db_dtypes/__init__.py

Co-authored-by: Tim Swast <swast@google.com>

Co-authored-by: Tim Swast <swast@google.com>
  • Loading branch information
chalmerlowe and tswast committed Jan 3, 2023
1 parent ce4a560 commit 357a315
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
15 changes: 14 additions & 1 deletion db_dtypes/__init__.py
Expand Up @@ -23,9 +23,11 @@
import packaging.version
import pandas
import pandas.api.extensions
from pandas.errors import OutOfBoundsDatetime
import pyarrow
import pyarrow.compute


from db_dtypes.version import __version__
from db_dtypes import core

Expand Down Expand Up @@ -143,6 +145,7 @@ def _datetime(
second = parsed.group("seconds")
fraction = parsed.group("fraction")
nanosecond = int(fraction.ljust(9, "0")[:9]) if fraction else 0

return pandas.Timestamp(
year=1970,
month=1,
Expand Down Expand Up @@ -263,7 +266,17 @@ def _datetime(
year = int(match.group("year"))
month = int(match.group("month"))
day = int(match.group("day"))
return pandas.Timestamp(year=year, month=month, day=day).to_datetime64()

dateObj = pandas.Timestamp(
year=year,
month=month,
day=day,
)
if pandas.Timestamp.min < dateObj < pandas.Timestamp.max:
return dateObj.to_datetime64()
else: # pragma: NO COVER
# TODO(#166): Include these lines in coverage when pandas 2.0 is released.
raise OutOfBoundsDatetime("Out of bounds", scalar) # pragma: NO COVER
else:
raise TypeError("Invalid value type", scalar)

Expand Down
17 changes: 14 additions & 3 deletions tests/unit/test_date.py
Expand Up @@ -18,6 +18,7 @@
import numpy
import numpy.testing
import pandas
from pandas.errors import OutOfBoundsDatetime
import pandas.testing
import pytest

Expand Down Expand Up @@ -143,15 +144,25 @@ def test_date_set_slice_null():
("2021-2-99", "day is out of range for month"),
("2021-99-1", "month must be in 1[.][.]12"),
("10000-1-1", "year 10000 is out of range"),
# Outside of min/max values pandas.Timestamp.
],
)
def test_date_parsing_errors(value, error):
with pytest.raises(ValueError, match=error):
pandas.Series([value], dtype="dbdate")


@pytest.mark.parametrize(
"value, error",
[
# Values that are outside of the min/max values allowed by pandas.Timestamp
("0001-01-01", "Out of bounds"),
("9999-12-31", "Out of bounds"),
("1677-09-21", "Out of bounds"),
("2262-04-12", "Out of bounds"),
],
)
def test_date_parsing_errors(value, error):
with pytest.raises(ValueError, match=error):
def test_date_parsing_errors_out_of_bounds(value, error):
with pytest.raises(OutOfBoundsDatetime, match=error):
pandas.Series([value], dtype="dbdate")


Expand Down

0 comments on commit 357a315

Please sign in to comment.