Skip to content

Commit 35923c0

Browse files
committed
Change exception string, move str check out
1 parent f03490e commit 35923c0

File tree

4 files changed

+12
-13
lines changed

4 files changed

+12
-13
lines changed

pandas/_libs/tslibs/timedeltas.pyx

+6-7
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,17 @@ def array_to_timedelta64(object[:] values, unit=None, errors='raise'):
234234
n = values.shape[0]
235235
result = np.empty(n, dtype='m8[ns]')
236236
iresult = result.view('i8')
237-
has_string = False
237+
238+
if unit is not None:
239+
for i in range(n):
240+
if isinstance(values[i], str):
241+
raise ValueError("unit must not be specified if the input contains a str")
238242

239243
# Usually, we have all strings. If so, we hit the fast path.
240244
# If this path fails, we try conversion a different way, and
241245
# this is where all of the error handling will take place.
242246
try:
243247
for i in range(n):
244-
if isinstance(values[i], str):
245-
has_string = True
246248
if values[i] is NaT:
247249
# we allow this check in the fast-path because NaT is a C-object
248250
# so this is an inexpensive check
@@ -260,9 +262,6 @@ def array_to_timedelta64(object[:] values, unit=None, errors='raise'):
260262
else:
261263
raise
262264

263-
if has_string and unit is not None:
264-
raise ValueError("unit must be un-specified if the input contains a str")
265-
266265
return iresult.base # .base to access underlying np.ndarray
267266

268267

@@ -1162,7 +1161,7 @@ class Timedelta(_Timedelta):
11621161
value = value.value
11631162
elif isinstance(value, str):
11641163
if unit is not None:
1165-
raise ValueError("unit must be un-specified if the value is a str")
1164+
raise ValueError("unit must not be specified if the value is a str")
11661165
if len(value) > 0 and value[0] == 'P':
11671166
value = parse_iso_format_string(value)
11681167
else:

pandas/core/arrays/timedeltas.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1014,7 +1014,7 @@ def objects_to_td64ns(data, unit=None, errors="raise"):
10141014
data : ndarray or Index
10151015
unit : str, default "ns"
10161016
The timedelta unit to treat integers as multiples of.
1017-
Must be un-specified if the data contains a str.
1017+
Must not be specified if the data contains a str.
10181018
errors : {"raise", "coerce", "ignore"}, default "raise"
10191019
How to handle elements that cannot be converted to timedelta64[ns].
10201020
See ``pandas.to_timedelta`` for details.

pandas/core/tools/timedeltas.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def to_timedelta(arg, unit=None, errors="raise"):
2727
arg : str, timedelta, list-like or Series
2828
The data to be converted to timedelta.
2929
unit : str, default 'ns'
30-
Must be un-specified if the arg is/contains a str.
30+
Must not be specified if the arg is/contains a str.
3131
Denotes the unit of the arg. Possible values:
3232
('W', 'D', 'days', 'day', 'hours', hour', 'hr', 'h',
3333
'm', 'minute', 'min', 'minutes', 'T', 'S', 'seconds',
@@ -107,7 +107,7 @@ def to_timedelta(arg, unit=None, errors="raise"):
107107
)
108108

109109
if isinstance(arg, str) and unit is not None:
110-
raise ValueError("unit must be un-specified if the input is/contains a str")
110+
raise ValueError("unit must not be specified if the input is/contains a str")
111111

112112
# ...so it must be a scalar value. Return scalar.
113113
return _coerce_scalar_to_timedelta_type(arg, unit=unit, errors=errors)

pandas/tests/scalar/timedelta/test_constructors.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,9 @@ def test_timedelta_constructor_identity():
294294
@pytest.mark.parametrize(
295295
"constructor, value, unit, expectation",
296296
[
297-
(Timedelta, "10s", "ms", (ValueError, "unit must be un-specified")),
298-
(to_timedelta, "10s", "ms", (ValueError, "unit must be un-specified")),
299-
(to_timedelta, ["1", 2, 3], "s", (ValueError, "unit must be un-specified")),
297+
(Timedelta, "10s", "ms", (ValueError, "unit must not be specified")),
298+
(to_timedelta, "10s", "ms", (ValueError, "unit must not be specified")),
299+
(to_timedelta, ["1", 2, 3], "s", (ValueError, "unit must not be specified")),
300300
],
301301
)
302302
def test_string_with_unit(constructor, value, unit, expectation):

0 commit comments

Comments
 (0)