Skip to content

Commit 62b26a7

Browse files
Regression in to_timedelta with errors="coerce" and unit (#34806)
1 parent 1c6f544 commit 62b26a7

File tree

4 files changed

+34
-13
lines changed

4 files changed

+34
-13
lines changed

pandas/_libs/tslibs/timedeltas.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def array_to_timedelta64(object[:] values, unit=None, errors='raise'):
237237

238238
if unit is not None:
239239
for i in range(n):
240-
if isinstance(values[i], str):
240+
if isinstance(values[i], str) and errors != "coerce":
241241
raise ValueError(
242242
"unit must not be specified if the input contains a str"
243243
)

pandas/core/arrays/timedeltas.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -882,9 +882,10 @@ def sequence_to_td64ns(data, copy=False, unit=None, errors="raise"):
882882
----------
883883
data : list-like
884884
copy : bool, default False
885-
unit : str, default "ns"
886-
The timedelta unit to treat integers as multiples of.
887-
Must be un-specifed if the data contains a str.
885+
unit : str, optional
886+
The timedelta unit to treat integers as multiples of. For numeric
887+
data this defaults to ``'ns'``.
888+
Must be un-specified if the data contains a str and ``errors=="raise"``.
888889
errors : {"raise", "coerce", "ignore"}, default "raise"
889890
How to handle elements that cannot be converted to timedelta64[ns].
890891
See ``pandas.to_timedelta`` for details.

pandas/core/tools/timedeltas.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,24 @@ def to_timedelta(arg, unit=None, errors="raise"):
2626
----------
2727
arg : str, timedelta, list-like or Series
2828
The data to be converted to timedelta.
29-
unit : str, default 'ns'
30-
Must not be specified if the arg is/contains a str.
31-
Denotes the unit of the arg. Possible values:
32-
('W', 'D', 'days', 'day', 'hours', hour', 'hr', 'h',
33-
'm', 'minute', 'min', 'minutes', 'T', 'S', 'seconds',
34-
'sec', 'second', 'ms', 'milliseconds', 'millisecond',
35-
'milli', 'millis', 'L', 'us', 'microseconds', 'microsecond',
36-
'micro', 'micros', 'U', 'ns', 'nanoseconds', 'nano', 'nanos',
37-
'nanosecond', 'N').
29+
unit : str, optional
30+
Denotes the unit of the arg for numeric `arg`. Defaults to ``"ns"``.
31+
32+
Possible values:
33+
34+
* 'W'
35+
* 'D' / 'days' / 'day'
36+
* 'hours' / 'hour' / 'hr' / 'h'
37+
* 'm' / 'minute' / 'min' / 'minutes' / 'T'
38+
* 'S' / 'seconds' / 'sec' / 'second'
39+
* 'ms' / 'milliseconds' / 'millisecond' / 'milli' / 'millis' / 'L'
40+
* 'us' / 'microseconds' / 'microsecond' / 'micro' / 'micros' / 'U'
41+
* 'ns' / 'nanoseconds' / 'nano' / 'nanos' / 'nanosecond' / 'N'
42+
43+
.. versionchanged:: 1.1.0
44+
45+
Must not be specified when `arg` context strings and
46+
``errors="raise"``.
3847
3948
errors : {'ignore', 'raise', 'coerce'}, default 'raise'
4049
- If 'raise', then invalid parsing will raise an exception.

pandas/tests/tools/test_to_timedelta.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,14 @@ def test_to_timedelta_float(self):
155155
result = pd.to_timedelta(arr, unit="s")
156156
expected_asi8 = np.arange(999990000, int(1e9), 1000, dtype="int64")
157157
tm.assert_numpy_array_equal(result.asi8, expected_asi8)
158+
159+
def test_to_timedelta_coerce_strings_unit(self):
160+
arr = np.array([1, 2, "error"], dtype=object)
161+
result = pd.to_timedelta(arr, unit="ns", errors="coerce")
162+
expected = pd.to_timedelta([1, 2, pd.NaT], unit="ns")
163+
tm.assert_index_equal(result, expected)
164+
165+
def test_to_timedelta_ignore_strings_unit(self):
166+
arr = np.array([1, 2, "error"], dtype=object)
167+
result = pd.to_timedelta(arr, unit="ns", errors="ignore")
168+
tm.assert_numpy_array_equal(result, arr)

0 commit comments

Comments
 (0)