Skip to content

REF: move misplaced to_time tests #32285

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

Merged
merged 2 commits into from
Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
48 changes: 1 addition & 47 deletions pandas/tests/indexes/datetimes/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import calendar
from collections import deque
from datetime import datetime, time, timedelta
from datetime import datetime, timedelta
import locale

from dateutil.parser import parse
Expand Down Expand Up @@ -2112,52 +2112,6 @@ def test_parsers_timestring(self, cache):
assert result4 == exp_now
assert result5 == exp_now

@td.skip_if_has_locale
def test_parsers_time(self):
# GH11818
strings = [
"14:15",
"1415",
"2:15pm",
"0215pm",
"14:15:00",
"141500",
"2:15:00pm",
"021500pm",
time(14, 15),
]
expected = time(14, 15)

for time_string in strings:
assert tools.to_time(time_string) == expected

new_string = "14.15"
msg = r"Cannot convert arg \['14\.15'\] to a time"
with pytest.raises(ValueError, match=msg):
tools.to_time(new_string)
assert tools.to_time(new_string, format="%H.%M") == expected

arg = ["14:15", "20:20"]
expected_arr = [time(14, 15), time(20, 20)]
assert tools.to_time(arg) == expected_arr
assert tools.to_time(arg, format="%H:%M") == expected_arr
assert tools.to_time(arg, infer_time_format=True) == expected_arr
assert tools.to_time(arg, format="%I:%M%p", errors="coerce") == [None, None]

res = tools.to_time(arg, format="%I:%M%p", errors="ignore")
tm.assert_numpy_array_equal(res, np.array(arg, dtype=np.object_))

with pytest.raises(ValueError):
tools.to_time(arg, format="%I:%M%p", errors="raise")

tm.assert_series_equal(
tools.to_time(Series(arg, name="test")), Series(expected_arr, name="test")
)

res = tools.to_time(np.array(arg))
assert isinstance(res, list)
assert res == expected_arr

@pytest.mark.parametrize("cache", [True, False])
@pytest.mark.parametrize(
"dt_string, tz, dt_string_repr",
Expand Down
58 changes: 58 additions & 0 deletions pandas/tests/tools/test_to_time.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from datetime import time

import numpy as np
import pytest

import pandas.util._test_decorators as td

from pandas import Series
import pandas._testing as tm
from pandas.core.tools.datetimes import to_time


class TestToTime:
@td.skip_if_has_locale
def test_parsers_time(self):
# GH#11818
strings = [
"14:15",
"1415",
"2:15pm",
"0215pm",
"14:15:00",
"141500",
"2:15:00pm",
"021500pm",
time(14, 15),
]
expected = time(14, 15)

for time_string in strings:
assert to_time(time_string) == expected

new_string = "14.15"
msg = r"Cannot convert arg \['14\.15'\] to a time"
with pytest.raises(ValueError, match=msg):
to_time(new_string)
assert to_time(new_string, format="%H.%M") == expected

arg = ["14:15", "20:20"]
expected_arr = [time(14, 15), time(20, 20)]
assert to_time(arg) == expected_arr
assert to_time(arg, format="%H:%M") == expected_arr
assert to_time(arg, infer_time_format=True) == expected_arr
assert to_time(arg, format="%I:%M%p", errors="coerce") == [None, None]

res = to_time(arg, format="%I:%M%p", errors="ignore")
tm.assert_numpy_array_equal(res, np.array(arg, dtype=np.object_))

with pytest.raises(ValueError):
to_time(arg, format="%I:%M%p", errors="raise")

tm.assert_series_equal(
to_time(Series(arg, name="test")), Series(expected_arr, name="test")
)

res = to_time(np.array(arg))
assert isinstance(res, list)
assert res == expected_arr