forked from pola-rs/polars
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rust, python): raise if to_datetime would have parsed input incor…
…rectly (pola-rs#9675)
- Loading branch information
1 parent
ed58c7a
commit 90c988c
Showing
4 changed files
with
163 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
py-polars/tests/parametric/time_series/test_to_datetime.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from datetime import datetime | ||
|
||
import hypothesis.strategies as st | ||
from hypothesis import given | ||
|
||
import polars as pl | ||
from polars.exceptions import ComputeError | ||
from polars.testing.parametric.strategies import strategy_datetime_format | ||
|
||
|
||
@given( | ||
datetimes=st.datetimes( | ||
min_value=datetime(2000, 1, 1), max_value=datetime(9999, 12, 31) | ||
), | ||
fmt=strategy_datetime_format(), | ||
) | ||
def test_to_datetime(datetimes: datetime, fmt: str) -> None: | ||
input = datetimes.strftime(fmt) | ||
expected = datetime.strptime(input, fmt) | ||
try: | ||
result = pl.Series([input]).str.to_datetime(format=fmt).item() | ||
except ComputeError as exc: | ||
# If there's an exception, check that it's either: | ||
# - something which polars can't parse at all: missing day or month | ||
# - something on which polars intentionally raises | ||
assert ( # noqa: PT017 | ||
( | ||
(("%H" in fmt) ^ ("%M" in fmt)) | ||
or (("%I" in fmt) ^ ("%M" in fmt)) | ||
or ("%S" in fmt and "%H" not in fmt) | ||
or ("%S" in fmt and "%I" not in fmt) | ||
or (("%I" in fmt) ^ ("%p" in fmt)) | ||
or (("%H" in fmt) ^ ("%p" in fmt)) | ||
) | ||
and "Invalid format string" in str(exc) | ||
) or ( | ||
( | ||
not any(day in fmt for day in ("%d", "%j")) | ||
or not any(month in fmt for month in ("%b", "%B", "%m")) | ||
) | ||
and "strict conversion to datetimes failed" in str(exc) | ||
) | ||
else: | ||
assert result == expected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters