Skip to content

Commit 217a428

Browse files
authored
TST: test_to_time (#32285)
1 parent 674dd69 commit 217a428

File tree

2 files changed

+59
-47
lines changed

2 files changed

+59
-47
lines changed

pandas/tests/indexes/datetimes/test_tools.py

+1-47
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import calendar
44
from collections import deque
5-
from datetime import datetime, time, timedelta
5+
from datetime import datetime, timedelta
66
import locale
77

88
from dateutil.parser import parse
@@ -2112,52 +2112,6 @@ def test_parsers_timestring(self, cache):
21122112
assert result4 == exp_now
21132113
assert result5 == exp_now
21142114

2115-
@td.skip_if_has_locale
2116-
def test_parsers_time(self):
2117-
# GH11818
2118-
strings = [
2119-
"14:15",
2120-
"1415",
2121-
"2:15pm",
2122-
"0215pm",
2123-
"14:15:00",
2124-
"141500",
2125-
"2:15:00pm",
2126-
"021500pm",
2127-
time(14, 15),
2128-
]
2129-
expected = time(14, 15)
2130-
2131-
for time_string in strings:
2132-
assert tools.to_time(time_string) == expected
2133-
2134-
new_string = "14.15"
2135-
msg = r"Cannot convert arg \['14\.15'\] to a time"
2136-
with pytest.raises(ValueError, match=msg):
2137-
tools.to_time(new_string)
2138-
assert tools.to_time(new_string, format="%H.%M") == expected
2139-
2140-
arg = ["14:15", "20:20"]
2141-
expected_arr = [time(14, 15), time(20, 20)]
2142-
assert tools.to_time(arg) == expected_arr
2143-
assert tools.to_time(arg, format="%H:%M") == expected_arr
2144-
assert tools.to_time(arg, infer_time_format=True) == expected_arr
2145-
assert tools.to_time(arg, format="%I:%M%p", errors="coerce") == [None, None]
2146-
2147-
res = tools.to_time(arg, format="%I:%M%p", errors="ignore")
2148-
tm.assert_numpy_array_equal(res, np.array(arg, dtype=np.object_))
2149-
2150-
with pytest.raises(ValueError):
2151-
tools.to_time(arg, format="%I:%M%p", errors="raise")
2152-
2153-
tm.assert_series_equal(
2154-
tools.to_time(Series(arg, name="test")), Series(expected_arr, name="test")
2155-
)
2156-
2157-
res = tools.to_time(np.array(arg))
2158-
assert isinstance(res, list)
2159-
assert res == expected_arr
2160-
21612115
@pytest.mark.parametrize("cache", [True, False])
21622116
@pytest.mark.parametrize(
21632117
"dt_string, tz, dt_string_repr",

pandas/tests/tools/test_to_time.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from datetime import time
2+
3+
import numpy as np
4+
import pytest
5+
6+
import pandas.util._test_decorators as td
7+
8+
from pandas import Series
9+
import pandas._testing as tm
10+
from pandas.core.tools.datetimes import to_time
11+
12+
13+
class TestToTime:
14+
@td.skip_if_has_locale
15+
def test_parsers_time(self):
16+
# GH#11818
17+
strings = [
18+
"14:15",
19+
"1415",
20+
"2:15pm",
21+
"0215pm",
22+
"14:15:00",
23+
"141500",
24+
"2:15:00pm",
25+
"021500pm",
26+
time(14, 15),
27+
]
28+
expected = time(14, 15)
29+
30+
for time_string in strings:
31+
assert to_time(time_string) == expected
32+
33+
new_string = "14.15"
34+
msg = r"Cannot convert arg \['14\.15'\] to a time"
35+
with pytest.raises(ValueError, match=msg):
36+
to_time(new_string)
37+
assert to_time(new_string, format="%H.%M") == expected
38+
39+
arg = ["14:15", "20:20"]
40+
expected_arr = [time(14, 15), time(20, 20)]
41+
assert to_time(arg) == expected_arr
42+
assert to_time(arg, format="%H:%M") == expected_arr
43+
assert to_time(arg, infer_time_format=True) == expected_arr
44+
assert to_time(arg, format="%I:%M%p", errors="coerce") == [None, None]
45+
46+
res = to_time(arg, format="%I:%M%p", errors="ignore")
47+
tm.assert_numpy_array_equal(res, np.array(arg, dtype=np.object_))
48+
49+
with pytest.raises(ValueError):
50+
to_time(arg, format="%I:%M%p", errors="raise")
51+
52+
tm.assert_series_equal(
53+
to_time(Series(arg, name="test")), Series(expected_arr, name="test")
54+
)
55+
56+
res = to_time(np.array(arg))
57+
assert isinstance(res, list)
58+
assert res == expected_arr

0 commit comments

Comments
 (0)