Skip to content

Commit d491cce

Browse files
Update test_unit_parser in test_timedelta.py
1 parent bc03065 commit d491cce

File tree

1 file changed

+104
-95
lines changed

1 file changed

+104
-95
lines changed

pandas/tests/scalar/timedelta/test_timedelta.py

Lines changed: 104 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -160,110 +160,119 @@ def test_nat_converters(self):
160160
assert result.astype("int64") == iNaT
161161

162162
@pytest.mark.parametrize(
163-
"units, np_unit",
164-
[
165-
(["W", "w"], "W"),
166-
(["D", "d", "days", "day", "Days", "Day"], "D"),
167-
(
168-
["m", "minute", "min", "minutes", "t", "Minute", "Min", "Minutes", "T"],
163+
"unit, np_unit, warning",
164+
[(value, "W", False) for value in ["W", "w"]]
165+
+ [(value, "D", False) for value in ["D", "d", "days", "day", "Days", "Day"]]
166+
+ [
167+
(value, "m", FutureWarning if value == "m" else False)
168+
for value in [
169169
"m",
170-
),
171-
(["s", "seconds", "sec", "second", "S", "Seconds", "Sec", "Second"], "s"),
172-
(
173-
[
174-
"ms",
175-
"milliseconds",
176-
"millisecond",
177-
"milli",
178-
"millis",
179-
"l",
180-
"MS",
181-
"Milliseconds",
182-
"Millisecond",
183-
"Milli",
184-
"Millis",
185-
"L",
186-
],
170+
"minute",
171+
"min",
172+
"minutes",
173+
"t",
174+
"Minute",
175+
"Min",
176+
"Minutes",
177+
"T",
178+
]
179+
]
180+
+ [
181+
(value, "s", False)
182+
for value in [
183+
"s",
184+
"seconds",
185+
"sec",
186+
"second",
187+
"S",
188+
"Seconds",
189+
"Sec",
190+
"Second",
191+
]
192+
]
193+
+ [
194+
(value, "ms", False)
195+
for value in [
187196
"ms",
188-
),
189-
(
190-
[
191-
"us",
192-
"microseconds",
193-
"microsecond",
194-
"micro",
195-
"micros",
196-
"u",
197-
"US",
198-
"Microseconds",
199-
"Microsecond",
200-
"Micro",
201-
"Micros",
202-
"U",
203-
],
197+
"milliseconds",
198+
"millisecond",
199+
"milli",
200+
"millis",
201+
"l",
202+
"MS",
203+
"Milliseconds",
204+
"Millisecond",
205+
"Milli",
206+
"Millis",
207+
"L",
208+
]
209+
]
210+
+ [
211+
(value, "us", False)
212+
for value in [
204213
"us",
205-
),
206-
(
207-
[
208-
"ns",
209-
"nanoseconds",
210-
"nanosecond",
211-
"nano",
212-
"nanos",
213-
"n",
214-
"NS",
215-
"Nanoseconds",
216-
"Nanosecond",
217-
"Nano",
218-
"Nanos",
219-
"N",
220-
],
214+
"microseconds",
215+
"microsecond",
216+
"micro",
217+
"micros",
218+
"u",
219+
"US",
220+
"Microseconds",
221+
"Microsecond",
222+
"Micro",
223+
"Micros",
224+
"U",
225+
]
226+
]
227+
+ [
228+
(value, "ns", False)
229+
for value in [
221230
"ns",
222-
),
231+
"nanoseconds",
232+
"nanosecond",
233+
"nano",
234+
"nanos",
235+
"n",
236+
"NS",
237+
"Nanoseconds",
238+
"Nanosecond",
239+
"Nano",
240+
"Nanos",
241+
"N",
242+
]
223243
],
224244
)
225245
@pytest.mark.parametrize("wrapper", [np.array, list, pd.Index])
226-
def test_unit_parser(self, units, np_unit, wrapper):
246+
def test_unit_parser(self, unit, np_unit, wrapper, warning):
227247
# validate all units, GH 6855, GH 21762
228-
for unit in units:
229-
# array-likes
230-
expected = TimedeltaIndex(
231-
[np.timedelta64(i, np_unit) for i in np.arange(5).tolist()]
232-
)
233-
result = to_timedelta(wrapper(range(5)), unit=unit)
234-
tm.assert_index_equal(result, expected)
235-
result = TimedeltaIndex(wrapper(range(5)), unit=unit)
236-
tm.assert_index_equal(result, expected)
237-
238-
if unit == "M":
239-
# M is treated as minutes in string repr
240-
expected = TimedeltaIndex(
241-
[np.timedelta64(i, "m") for i in np.arange(5).tolist()]
242-
)
243-
244-
str_repr = [f"{x}{unit}" for x in np.arange(5)]
248+
# array-likes
249+
expected = TimedeltaIndex(
250+
[np.timedelta64(i, np_unit) for i in np.arange(5).tolist()]
251+
)
252+
result = to_timedelta(wrapper(range(5)), unit=unit)
253+
tm.assert_index_equal(result, expected)
254+
result = TimedeltaIndex(wrapper(range(5)), unit=unit)
255+
tm.assert_index_equal(result, expected)
256+
257+
str_repr = [f"{x}{unit}" for x in np.arange(5)]
258+
with tm.assert_produces_warning(warning):
245259
result = to_timedelta(wrapper(str_repr))
246-
tm.assert_index_equal(result, expected)
247-
result = TimedeltaIndex(wrapper(str_repr))
248-
tm.assert_index_equal(result, expected)
249-
250-
# scalar
251-
expected = Timedelta(np.timedelta64(2, np_unit).astype("timedelta64[ns]"))
252-
253-
result = to_timedelta(2, unit=unit)
254-
assert result == expected
255-
result = Timedelta(2, unit=unit)
256-
assert result == expected
257-
258-
if unit == "M":
259-
expected = Timedelta(np.timedelta64(2, "m").astype("timedelta64[ns]"))
260-
261-
warning = None if unit != "m" else FutureWarning
262-
with tm.assert_produces_warning(warning):
263-
result = to_timedelta(f"2{unit}")
264-
assert result == expected
265-
result = Timedelta(f"2{unit}")
266-
assert result == expected
260+
tm.assert_index_equal(result, expected)
261+
result = TimedeltaIndex(wrapper(str_repr))
262+
tm.assert_index_equal(result, expected)
263+
264+
# scalar
265+
expected = Timedelta(np.timedelta64(2, np_unit).astype("timedelta64[ns]"))
266+
result = to_timedelta(2, unit=unit)
267+
assert result == expected
268+
result = Timedelta(2, unit=unit)
269+
assert result == expected
270+
271+
with tm.assert_produces_warning(warning):
272+
result = to_timedelta(f"2{unit}")
273+
assert result == expected
274+
result = Timedelta(f"2{unit}")
275+
assert result == expected
267276

268277
@pytest.mark.parametrize("unit", ["Y", "y", "M"])
269278
def test_unit_m_y_raises(self, unit):

0 commit comments

Comments
 (0)