Skip to content

Commit d8c7fd7

Browse files
committed
raise FutureWarning instead of ValueError
1 parent ae2f843 commit d8c7fd7

File tree

5 files changed

+19
-11
lines changed

5 files changed

+19
-11
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ Deprecations
279279
- Deprecated constructing :class:`SparseArray` from scalar data, pass a sequence instead (:issue:`53039`)
280280
- Deprecated option "mode.use_inf_as_na", convert inf entries to ``NaN`` before instead (:issue:`51684`)
281281
- Deprecated positional indexing on :class:`Series` with :meth:`Series.__getitem__` and :meth:`Series.__setitem__`, in a future version ``ser[item]`` will *always* interpret ``item`` as a label, not a position (:issue:`50617`)
282+
- Deprecated strings ``T``, ``t``, ``L`` and ``l`` denoting units in :func:`to_timedelta` (:issue:`52536`)
282283

283284
.. ---------------------------------------------------------------------------
284285
.. _whatsnew_210.performance:

pandas/_libs/tslibs/dtypes.pyx

-4
Original file line numberDiff line numberDiff line change
@@ -379,14 +379,10 @@ cpdef NPY_DATETIMEUNIT abbrev_to_npy_unit(str abbrev):
379379
return NPY_DATETIMEUNIT.NPY_FR_h
380380
elif abbrev == "m":
381381
return NPY_DATETIMEUNIT.NPY_FR_m
382-
elif abbrev == "T" or abbrev == "t":
383-
return NPY_DATETIMEUNIT.NPY_FR_m
384382
elif abbrev == "s":
385383
return NPY_DATETIMEUNIT.NPY_FR_s
386384
elif abbrev == "ms":
387385
return NPY_DATETIMEUNIT.NPY_FR_ms
388-
elif abbrev == "L" or abbrev == "l":
389-
return NPY_DATETIMEUNIT.NPY_FR_ms
390386
elif abbrev == "us":
391387
return NPY_DATETIMEUNIT.NPY_FR_us
392388
elif abbrev == "ns":

pandas/_libs/tslibs/timedeltas.pyi

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ UnitChoices = Literal[
3737
"minute",
3838
"min",
3939
"minutes",
40-
"t",
4140
"T",
41+
"t",
4242
"s",
4343
"seconds",
4444
"sec",
@@ -48,8 +48,8 @@ UnitChoices = Literal[
4848
"millisecond",
4949
"milli",
5050
"millis",
51-
"l",
5251
"L",
52+
"l",
5353
"us",
5454
"microseconds",
5555
"microsecond",

pandas/_libs/tslibs/timedeltas.pyx

+3-1
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ cdef dict timedelta_abbrevs = {
123123
"minute": "m",
124124
"min": "m",
125125
"minutes": "m",
126+
"T": "m",
126127
"t": "m",
127128
"s": "s",
128129
"seconds": "s",
@@ -133,6 +134,7 @@ cdef dict timedelta_abbrevs = {
133134
"millisecond": "ms",
134135
"milli": "ms",
135136
"millis": "ms",
137+
"L": "ms",
136138
"l": "ms",
137139
"us": "us",
138140
"microseconds": "us",
@@ -722,7 +724,7 @@ cpdef inline str parse_timedelta_unit(str unit):
722724
"""
723725
if unit is None:
724726
return "ns"
725-
elif unit in {"M", "t", "T", "l", "L"}:
727+
elif unit == "M":
726728
return unit
727729
try:
728730
return timedelta_abbrevs[unit.lower()]

pandas/core/tools/timedeltas.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
TYPE_CHECKING,
88
overload,
99
)
10+
import warnings
1011

1112
import numpy as np
1213

@@ -19,6 +20,7 @@
1920
Timedelta,
2021
parse_timedelta_unit,
2122
)
23+
from pandas.util._exceptions import find_stack_level
2224

2325
from pandas.core.dtypes.common import is_list_like
2426
from pandas.core.dtypes.generic import (
@@ -110,14 +112,17 @@ def to_timedelta(
110112
* 'W'
111113
* 'D' / 'days' / 'day'
112114
* 'hours' / 'hour' / 'hr' / 'h'
113-
* 'm' / 'minute' / 'min' / 'minutes'
115+
* 'm' / 'minute' / 'min' / 'minutes' / 'T'
114116
* 'S' / 'seconds' / 'sec' / 'second'
115-
* 'ms' / 'milliseconds' / 'millisecond' / 'milli' / 'millis'
117+
* 'ms' / 'milliseconds' / 'millisecond' / 'milli' / 'millis' / 'L'
116118
* 'us' / 'microseconds' / 'microsecond' / 'micro' / 'micros' / 'U'
117119
* 'ns' / 'nanoseconds' / 'nano' / 'nanos' / 'nanosecond' / 'N'
118120
119121
Must not be specified when `arg` context strings and ``errors="raise"``.
120122
123+
.. deprecated:: 2.1.0
124+
Units 'T' and 'L' are deprecated and will be removed in a future version.
125+
121126
errors : {'ignore', 'raise', 'coerce'}, default 'raise'
122127
- If 'raise', then invalid parsing will raise an exception.
123128
- If 'coerce', then invalid parsing will be set as NaT.
@@ -181,8 +186,12 @@ def to_timedelta(
181186
"represent unambiguous timedelta values durations."
182187
)
183188

184-
if unit in {"t", "T", "l", "L"}:
185-
raise ValueError("Units 't', 'T', 'l' and 'L' are no longer supported.")
189+
if unit in {"T", "t", "L", "l"}:
190+
warnings.warn(
191+
f"Unit {unit} is deprecated and will be removed in a future version.",
192+
FutureWarning,
193+
stacklevel=find_stack_level(),
194+
)
186195

187196
if arg is None:
188197
return arg

0 commit comments

Comments
 (0)