Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: raise ValueError if date is out-of-bounds #46

Merged
merged 3 commits into from
Dec 4, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions db_dtypes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,15 @@ def _datetime(
if scalar is None:
return None
elif isinstance(scalar, datetime.date):
return datetime.datetime(scalar.year, scalar.month, scalar.day)
return pandas.Timestamp(scalar.year, scalar.month, scalar.day)
elif isinstance(scalar, str):
match = match_fn(scalar)
if not match:
raise ValueError(f"Bad date string: {repr(scalar)}")
year = int(match.group("year"))
month = int(match.group("month"))
day = int(match.group("day"))
return datetime.datetime(year, month, day)
return pandas.Timestamp(year, month, day)
else:
raise TypeError("Invalid value type", scalar)

Expand Down
3 changes: 2 additions & 1 deletion tests/unit/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def types_mapper(
type=pyarrow.time64("us"),
),
),
(
pytest.param(
pandas.Series(
[
# Only microseconds are supported when reading data. See:
Expand Down Expand Up @@ -216,6 +216,7 @@ def types_mapper(
],
type=pyarrow.time64("ns"),
),
id="time-nanoseconds-arrow-round-trip",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover from debugging why when I made the "time" _datetime method return a Timestamp too, this broke. Since that change isn't necessary and it broke this test I reverted it. Kept the test id, since I thought it was useful.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test ID is indeed useful IMO. 👍

),
]

Expand Down
5 changes: 5 additions & 0 deletions tests/unit/test_date.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ def test_date_parsing(value, expected):
("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.
("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):
Expand Down