Skip to content

Commit 7fafd4a

Browse files
committed
Edge case: threshold outside of [0.0, 1.0]
1 parent 0a76788 commit 7fafd4a

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

pandas/core/tools/datetimes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,9 @@ def to_datetime(
10291029
if arg is None:
10301030
return NaT
10311031

1032+
if not (0.0 <= threshold <= 1.0):
1033+
raise ValueError(f"`threshold` must be between 0.0 and 1.0, got {threshold}")
1034+
10321035
if origin != "unix":
10331036
arg = _adjust_to_origin(arg, origin, unit)
10341037

pandas/tests/tools/test_to_datetime.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4155,3 +4155,22 @@ def test_example(self):
41554155
threshold=0.5,
41564156
)
41574157
assert isna(result)
4158+
4159+
def test_bad_threshold(self):
4160+
with pytest.raises(
4161+
ValueError, match="`threshold` must be between 0.0 and 1.0, got -0.5"
4162+
):
4163+
_ = to_datetime(
4164+
"2020-01-01 12:20:20",
4165+
format="%Y-%m-%d %H:%M:%S",
4166+
threshold=-0.5,
4167+
)
4168+
4169+
with pytest.raises(
4170+
ValueError, match="`threshold` must be between 0.0 and 1.0, got 2.0"
4171+
):
4172+
_ = to_datetime(
4173+
"2020-01-01 12:20:20",
4174+
format="%Y-%m-%d %H:%M:%S",
4175+
threshold=2.0,
4176+
)

0 commit comments

Comments
 (0)