Skip to content

Commit ba76c2c

Browse files
Throw warning when M,Y,m or y is used in an arg for pd.to_timedelta
1 parent 40b2aa5 commit ba76c2c

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

pandas/core/tools/timedeltas.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
timedelta support tools
33
"""
4+
import warnings
45

56
import numpy as np
67

@@ -118,8 +119,18 @@ def to_timedelta(arg, unit=None, errors="raise"):
118119
"arg must be a string, timedelta, list, tuple, 1-d array, or Series"
119120
)
120121

121-
if isinstance(arg, str) and unit is not None:
122-
raise ValueError("unit must not be specified if the input is/contains a str")
122+
if isinstance(arg, str):
123+
if unit is not None:
124+
raise ValueError(
125+
"unit must not be specified if the input is/contains a str"
126+
)
127+
elif arg.upper().endswith(" M") or arg.upper().endswith(" Y"):
128+
warnings.warn(
129+
"'M', 'Y', 'm' and 'y' do not represent unambiguous timedelta values"
130+
" durations. and will removed in a future version",
131+
FutureWarning,
132+
stacklevel=2,
133+
)
123134

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

0 commit comments

Comments
 (0)