Skip to content

Commit bc03065

Browse files
Also find appearences of unambiguous timedelta values in containers
1 parent ad38ad3 commit bc03065

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

pandas/_libs/tslibs/timedeltas.pyx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import collections
2+
import re
23

34
import cython
45

@@ -1467,6 +1468,16 @@ cdef _broadcast_floordiv_td64(
14671468
return res
14681469

14691470

1471+
def check_unambiguous_timedelta_values(object[:] values):
1472+
cdef:
1473+
Py_ssize_t i, n = len(values)
1474+
1475+
for i in range(n):
1476+
if re.search(r"^\d+\s?[M|Y|m|y]$", str(values[i])):
1477+
return True
1478+
return False
1479+
1480+
14701481
# resolution in ns
14711482
Timedelta.min = Timedelta(np.iinfo(np.int64).min + 1)
14721483
Timedelta.max = Timedelta(np.iinfo(np.int64).max)

pandas/core/tools/timedeltas.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
import numpy as np
88

99
from pandas._libs.tslibs import NaT
10-
from pandas._libs.tslibs.timedeltas import Timedelta, parse_timedelta_unit
10+
from pandas._libs.tslibs.timedeltas import (
11+
Timedelta,
12+
check_unambiguous_timedelta_values,
13+
parse_timedelta_unit,
14+
)
1115

1216
from pandas.core.dtypes.common import is_list_like
1317
from pandas.core.dtypes.generic import ABCIndexClass, ABCSeries
@@ -106,6 +110,16 @@ def to_timedelta(arg, unit=None, errors="raise"):
106110
"represent unambiguous timedelta values durations."
107111
)
108112

113+
if isinstance(arg, (list, tuple, ABCSeries, ABCIndexClass, np.ndarray)):
114+
arr = np.array(arg, dtype="object", ndmin=1)
115+
if arr.ndim == 1 and check_unambiguous_timedelta_values(arr):
116+
warnings.warn(
117+
"Denoting units with 'M', 'Y', 'm' or 'y' do not represent unambiguous "
118+
"timedelta values durations and will removed in a future version",
119+
FutureWarning,
120+
stacklevel=2,
121+
)
122+
109123
if arg is None:
110124
return arg
111125
elif isinstance(arg, ABCSeries):

0 commit comments

Comments
 (0)