Skip to content

Commit d7f9d1c

Browse files
author
Victor
committed
Accepts integer/float string with units and raises when unit is ambiguous.
1 parent e65eed5 commit d7f9d1c

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

pandas/tests/scalar/timedelta/test_construction.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ def test_construction():
8585
with pytest.raises(ValueError):
8686
Timedelta('10 days -1 h 1.5m 1s 3us')
8787

88+
# no units specified
89+
with pytest.raises(ValueError):
90+
Timedelta('3.1415')
91+
8892
# invalid construction
8993
tm.assert_raises_regex(ValueError, "cannot construct a Timedelta",
9094
lambda: Timedelta())
@@ -219,15 +223,17 @@ def __exit__(self, exc_type, exc_val, exc_tb):
219223
@pytest.mark.parametrize("redundant_unit, expectation", [
220224
("", not_raises()),
221225
("d", pytest.raises(ValueError)),
222-
("us", pytest.raises(ValueError))])
226+
("us", pytest.raises(ValueError)),
227+
])
223228
@pytest.mark.parametrize("unit", [
224-
"d", "m", "s", "us"])
229+
"d", "m", "s", "us"
230+
])
225231
@pytest.mark.parametrize("sign", [
226-
+1, -1])
232+
+1, -1
233+
])
227234
@pytest.mark.parametrize("num", [
228-
0.001, 1, 10])
235+
0.001, 1, 10
236+
])
229237
def test_string_with_unit(num, sign, unit, redundant_unit, expectation):
230238
with expectation:
231-
val = sign * num
232-
val_str = str(val) + redundant_unit
233-
assert Timedelta(val_str, unit=unit) == Timedelta(val, unit=unit)
239+
assert Timedelta(str(sign*num)+redundant_unit, unit=unit) == Timedelta(sign*num, unit=unit)

0 commit comments

Comments
 (0)