Skip to content

Commit b7991da

Browse files
authored
CLN: test_to_timedelta.py (#44843)
1 parent f34a65b commit b7991da

File tree

1 file changed

+43
-52
lines changed

1 file changed

+43
-52
lines changed

pandas/tests/tools/test_to_timedelta.py

Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -30,52 +30,45 @@ def test_to_timedelta_readonly(self, readonly):
3030
expected = to_timedelta([])
3131
tm.assert_index_equal(result, expected)
3232

33-
def test_to_timedelta(self):
34-
33+
def test_to_timedelta_null(self):
3534
result = to_timedelta(["", ""])
3635
assert isna(result).all()
3736

37+
def test_to_timedelta_same_np_timedelta64(self):
3838
# pass thru
3939
result = to_timedelta(np.array([np.timedelta64(1, "s")]))
4040
expected = pd.Index(np.array([np.timedelta64(1, "s")]))
4141
tm.assert_index_equal(result, expected)
4242

43+
def test_to_timedelta_series(self):
4344
# Series
4445
expected = Series([timedelta(days=1), timedelta(days=1, seconds=1)])
4546
result = to_timedelta(Series(["1d", "1days 00:00:01"]))
4647
tm.assert_series_equal(result, expected)
4748

49+
def test_to_timedelta_units(self):
4850
# with units
4951
result = TimedeltaIndex(
5052
[np.timedelta64(0, "ns"), np.timedelta64(10, "s").astype("m8[ns]")]
5153
)
5254
expected = to_timedelta([0, 10], unit="s")
5355
tm.assert_index_equal(result, expected)
5456

57+
@pytest.mark.parametrize(
58+
"dtype, unit",
59+
[
60+
["int64", "s"],
61+
["int64", "m"],
62+
["int64", "h"],
63+
["timedelta64[s]", "s"],
64+
["timedelta64[D]", "D"],
65+
],
66+
)
67+
def test_to_timedelta_units_dtypes(self, dtype, unit):
5568
# arrays of various dtypes
56-
arr = np.array([1] * 5, dtype="int64")
57-
result = to_timedelta(arr, unit="s")
58-
expected = TimedeltaIndex([np.timedelta64(1, "s")] * 5)
59-
tm.assert_index_equal(result, expected)
60-
61-
arr = np.array([1] * 5, dtype="int64")
62-
result = to_timedelta(arr, unit="m")
63-
expected = TimedeltaIndex([np.timedelta64(1, "m")] * 5)
64-
tm.assert_index_equal(result, expected)
65-
66-
arr = np.array([1] * 5, dtype="int64")
67-
result = to_timedelta(arr, unit="h")
68-
expected = TimedeltaIndex([np.timedelta64(1, "h")] * 5)
69-
tm.assert_index_equal(result, expected)
70-
71-
arr = np.array([1] * 5, dtype="timedelta64[s]")
72-
result = to_timedelta(arr)
73-
expected = TimedeltaIndex([np.timedelta64(1, "s")] * 5)
74-
tm.assert_index_equal(result, expected)
75-
76-
arr = np.array([1] * 5, dtype="timedelta64[D]")
77-
result = to_timedelta(arr)
78-
expected = TimedeltaIndex([np.timedelta64(1, "D")] * 5)
69+
arr = np.array([1] * 5, dtype=dtype)
70+
result = to_timedelta(arr, unit=unit)
71+
expected = TimedeltaIndex([np.timedelta64(1, unit)] * 5)
7972
tm.assert_index_equal(result, expected)
8073

8174
def test_to_timedelta_oob_non_nano(self):
@@ -91,31 +84,30 @@ def test_to_timedelta_oob_non_nano(self):
9184
with pytest.raises(OutOfBoundsTimedelta, match=msg):
9285
TimedeltaArray._from_sequence(arr)
9386

94-
def test_to_timedelta_dataframe(self):
87+
@pytest.mark.parametrize(
88+
"arg", [np.arange(10).reshape(2, 5), pd.DataFrame(np.arange(10).reshape(2, 5))]
89+
)
90+
@pytest.mark.parametrize("errors", ["ignore", "raise", "coerce"])
91+
def test_to_timedelta_dataframe(self, arg, errors):
9592
# GH 11776
96-
arr = np.arange(10).reshape(2, 5)
97-
df = pd.DataFrame(np.arange(10).reshape(2, 5))
98-
for arg in (arr, df):
99-
with pytest.raises(TypeError, match="1-d array"):
100-
to_timedelta(arg)
101-
for errors in ["ignore", "raise", "coerce"]:
102-
with pytest.raises(TypeError, match="1-d array"):
103-
to_timedelta(arg, errors=errors)
93+
with pytest.raises(TypeError, match="1-d array"):
94+
to_timedelta(arg, errors=errors)
10495

105-
def test_to_timedelta_invalid(self):
96+
def test_to_timedelta_invalid_errors(self):
10697

10798
# bad value for errors parameter
10899
msg = "errors must be one of"
109100
with pytest.raises(ValueError, match=msg):
110101
to_timedelta(["foo"], errors="never")
111102

103+
@pytest.mark.parametrize("arg", [[1, 2], 1])
104+
def test_to_timedelta_invalid_unit(self, arg):
112105
# these will error
113106
msg = "invalid unit abbreviation: foo"
114107
with pytest.raises(ValueError, match=msg):
115-
to_timedelta([1, 2], unit="foo")
116-
with pytest.raises(ValueError, match=msg):
117-
to_timedelta(1, unit="foo")
108+
to_timedelta(arg, unit="foo")
118109

110+
def test_to_timedelta_time(self):
119111
# time not supported ATM
120112
msg = (
121113
"Value must be Timedelta, string, integer, float, timedelta or convertible"
@@ -124,10 +116,12 @@ def test_to_timedelta_invalid(self):
124116
to_timedelta(time(second=1))
125117
assert to_timedelta(time(second=1), errors="coerce") is pd.NaT
126118

119+
def test_to_timedelta_bad_value(self):
127120
msg = "Could not convert 'foo' to NumPy timedelta"
128121
with pytest.raises(ValueError, match=msg):
129122
to_timedelta(["foo", "bar"])
130123

124+
def test_to_timedelta_bad_value_coerce(self):
131125
tm.assert_index_equal(
132126
TimedeltaIndex([pd.NaT, pd.NaT]),
133127
to_timedelta(["foo", "bar"], errors="coerce"),
@@ -138,6 +132,7 @@ def test_to_timedelta_invalid(self):
138132
to_timedelta(["1 day", "bar", "1 min"], errors="coerce"),
139133
)
140134

135+
def test_to_timedelta_invalid_errors_ignore(self):
141136
# gh-13613: these should not error because errors='ignore'
142137
invalid_data = "apple"
143138
assert invalid_data == to_timedelta(invalid_data, errors="ignore")
@@ -213,11 +208,10 @@ def test_to_timedelta_on_missing_values(self):
213208
actual = to_timedelta(ser)
214209
tm.assert_series_equal(actual, expected)
215210

216-
actual = to_timedelta(np.nan)
217-
assert actual.value == timedelta_NaT.astype("int64")
218-
219-
actual = to_timedelta(pd.NaT)
220-
assert actual.value == timedelta_NaT.astype("int64")
211+
@pytest.mark.parametrize("val", [np.nan, pd.NaT])
212+
def test_to_timedelta_on_missing_values_scalar(self, val):
213+
actual = to_timedelta(val)
214+
assert actual.value == np.timedelta64("NaT").astype("int64")
221215

222216
def test_to_timedelta_float(self):
223217
# https://github.com/pandas-dev/pandas/issues/25077
@@ -237,16 +231,13 @@ def test_to_timedelta_ignore_strings_unit(self):
237231
result = to_timedelta(arr, unit="ns", errors="ignore")
238232
tm.assert_numpy_array_equal(result, arr)
239233

240-
def test_to_timedelta_nullable_int64_dtype(self):
234+
@pytest.mark.parametrize(
235+
"expected_val, result_val", [[timedelta(days=2), 2], [None, None]]
236+
)
237+
def test_to_timedelta_nullable_int64_dtype(self, expected_val, result_val):
241238
# GH 35574
242-
expected = Series([timedelta(days=1), timedelta(days=2)])
243-
result = to_timedelta(Series([1, 2], dtype="Int64"), unit="days")
244-
245-
tm.assert_series_equal(result, expected)
246-
247-
# IntegerArray Series with nulls
248-
expected = Series([timedelta(days=1), None])
249-
result = to_timedelta(Series([1, None], dtype="Int64"), unit="days")
239+
expected = Series([timedelta(days=1), expected_val])
240+
result = to_timedelta(Series([1, result_val], dtype="Int64"), unit="days")
250241

251242
tm.assert_series_equal(result, expected)
252243

0 commit comments

Comments
 (0)