Skip to content

Commit 73ea3c1

Browse files
authored
DEPR: deprecate units 'w', 'd', 'MS', 'US', 'NS' for Timedelta in favor of 'W', 'D', 'ms', 'us', 'ns' (#59051)
* deprecate lower/uppercase units for Timedelta * correct examples in timedeltas.rst, add a note to v.3.0.0 * fix tests * add tests, fix tests, corrected docs examples * correct docs * fix an example in v0.13.0
1 parent 214ac73 commit 73ea3c1

31 files changed

+192
-94
lines changed

doc/source/user_guide/timedeltas.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ You can construct a ``Timedelta`` scalar through various arguments, including `I
3535
pd.Timedelta(days=1, seconds=1)
3636
3737
# integers with a unit
38-
pd.Timedelta(1, unit="d")
38+
pd.Timedelta(1, unit="D")
3939
4040
# from a datetime.timedelta/np.timedelta64
4141
pd.Timedelta(datetime.timedelta(days=1, seconds=1))
@@ -94,7 +94,7 @@ is numeric:
9494
.. ipython:: python
9595
9696
pd.to_timedelta(np.arange(5), unit="s")
97-
pd.to_timedelta(np.arange(5), unit="d")
97+
pd.to_timedelta(np.arange(5), unit="D")
9898
9999
.. warning::
100100
If a string or array of strings is passed as an input then the ``unit`` keyword

doc/source/whatsnew/v0.13.0.rst

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -523,13 +523,25 @@ Enhancements
523523
Using the new top-level ``to_timedelta``, you can convert a scalar or array from the standard
524524
timedelta format (produced by ``to_csv``) into a timedelta type (``np.timedelta64`` in ``nanoseconds``).
525525

526-
.. ipython:: python
526+
.. code-block:: ipython
527+
528+
In [53]: pd.to_timedelta('1 days 06:05:01.00003')
529+
Out[53]: Timedelta('1 days 06:05:01.000030')
530+
531+
In [54]: pd.to_timedelta('15.5us')
532+
Out[54]: Timedelta('0 days 00:00:00.000015500')
533+
534+
In [55]: pd.to_timedelta(['1 days 06:05:01.00003', '15.5us', 'nan'])
535+
Out[55]: TimedeltaIndex(['1 days 06:05:01.000030', '0 days 00:00:00.000015500', NaT], dtype='timedelta64[ns]', freq=None)
536+
537+
In [56]: pd.to_timedelta(np.arange(5), unit='s')
538+
Out[56]:
539+
TimedeltaIndex(['0 days 00:00:00', '0 days 00:00:01', '0 days 00:00:02',
540+
'0 days 00:00:03', '0 days 00:00:04'],
541+
dtype='timedelta64[ns]', freq=None)
527542
528-
pd.to_timedelta('1 days 06:05:01.00003')
529-
pd.to_timedelta('15.5us')
530-
pd.to_timedelta(['1 days 06:05:01.00003', '15.5us', 'nan'])
531-
pd.to_timedelta(np.arange(5), unit='s')
532-
pd.to_timedelta(np.arange(5), unit='d')
543+
In [57]: pd.to_timedelta(np.arange(5), unit='d')
544+
Out[57]: TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'], dtype='timedelta64[ns]', freq=None)
533545
534546
A Series of dtype ``timedelta64[ns]`` can now be divided by another
535547
``timedelta64[ns]`` object, or astyped to yield a ``float64`` dtyped Series. This

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ Other Deprecations
273273
- Deprecated allowing non-keyword arguments in :meth:`Series.to_string` except ``buf``. (:issue:`57280`)
274274
- Deprecated behavior of :meth:`Series.dt.to_pytimedelta`, in a future version this will return a :class:`Series` containing python ``datetime.timedelta`` objects instead of an ``ndarray`` of timedelta; this matches the behavior of other :meth:`Series.dt` properties. (:issue:`57463`)
275275
- Deprecated parameter ``method`` in :meth:`DataFrame.reindex_like` / :meth:`Series.reindex_like` (:issue:`58667`)
276+
- Deprecated strings ``w``, ``d``, ``MIN``, ``MS``, ``US`` and ``NS`` denoting units in :class:`Timedelta` in favour of ``W``, ``D``, ``min``, ``ms``, ``us`` and ``ns`` (:issue:`59051`)
276277
- Deprecated using ``epoch`` date format in :meth:`DataFrame.to_json` and :meth:`Series.to_json`, use ``iso`` instead. (:issue:`57063`)
277278

278279
.. ---------------------------------------------------------------------------

pandas/_libs/tslibs/dtypes.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ cdef dict c_OFFSET_TO_PERIOD_FREQSTR
1515
cdef dict c_PERIOD_TO_OFFSET_FREQSTR
1616
cdef dict c_OFFSET_RENAMED_FREQSTR
1717
cdef dict c_DEPR_ABBREVS
18+
cdef dict c_DEPR_UNITS
1819
cdef dict c_PERIOD_AND_OFFSET_DEPR_FREQSTR
1920
cdef dict attrname_to_abbrevs
2021
cdef dict npy_unit_to_attrname

pandas/_libs/tslibs/dtypes.pyx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,17 @@ cdef dict c_DEPR_ABBREVS = {
346346
"S": "s",
347347
}
348348

349+
cdef dict c_DEPR_UNITS = {
350+
"w": "W",
351+
"d": "D",
352+
"H": "h",
353+
"MIN": "min",
354+
"S": "s",
355+
"MS": "ms",
356+
"US": "us",
357+
"NS": "ns",
358+
}
359+
349360
cdef dict c_PERIOD_AND_OFFSET_DEPR_FREQSTR = {
350361
"w": "W",
351362
"MIN": "min",

pandas/_libs/tslibs/timedeltas.pyx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ from pandas._libs.tslibs.conversion cimport (
4343
precision_from_unit,
4444
)
4545
from pandas._libs.tslibs.dtypes cimport (
46-
c_DEPR_ABBREVS,
46+
c_DEPR_UNITS,
4747
get_supported_reso,
4848
is_supported_unit,
4949
npy_unit_to_abbrev,
@@ -719,15 +719,15 @@ cpdef inline str parse_timedelta_unit(str unit):
719719
return "ns"
720720
elif unit == "M":
721721
return unit
722-
elif unit in c_DEPR_ABBREVS:
722+
elif unit in c_DEPR_UNITS:
723723
warnings.warn(
724724
f"\'{unit}\' is deprecated and will be removed in a "
725-
f"future version. Please use \'{c_DEPR_ABBREVS.get(unit)}\' "
725+
f"future version. Please use \'{c_DEPR_UNITS.get(unit)}\' "
726726
f"instead of \'{unit}\'.",
727727
FutureWarning,
728728
stacklevel=find_stack_level(),
729729
)
730-
unit = c_DEPR_ABBREVS[unit]
730+
unit = c_DEPR_UNITS[unit]
731731
try:
732732
return timedelta_abbrevs[unit.lower()]
733733
except KeyError:
@@ -1823,6 +1823,11 @@ class Timedelta(_Timedelta):
18231823
Values `H`, `T`, `S`, `L`, `U`, and `N` are deprecated in favour
18241824
of the values `h`, `min`, `s`, `ms`, `us`, and `ns`.
18251825
1826+
.. deprecated:: 3.0.0
1827+
1828+
Allowing the values `w`, `d`, `MIN`, `MS`, `US` and `NS` to denote units
1829+
are deprecated in favour of the values `W`, `D`, `min`, `ms`, `us` and `ns`.
1830+
18261831
**kwargs
18271832
Available kwargs: {days, seconds, microseconds,
18281833
milliseconds, minutes, hours, weeks}.

pandas/core/arrays/timedeltas.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ def total_seconds(self) -> npt.NDArray[np.float64]:
746746
--------
747747
**Series**
748748
749-
>>> s = pd.Series(pd.to_timedelta(np.arange(5), unit="d"))
749+
>>> s = pd.Series(pd.to_timedelta(np.arange(5), unit="D"))
750750
>>> s
751751
0 0 days
752752
1 1 days
@@ -765,7 +765,7 @@ def total_seconds(self) -> npt.NDArray[np.float64]:
765765
766766
**TimedeltaIndex**
767767
768-
>>> idx = pd.to_timedelta(np.arange(5), unit="d")
768+
>>> idx = pd.to_timedelta(np.arange(5), unit="D")
769769
>>> idx
770770
TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'],
771771
dtype='timedelta64[ns]', freq=None)
@@ -809,7 +809,7 @@ def to_pytimedelta(self) -> npt.NDArray[np.object_]:
809809
--------
810810
For Series:
811811
812-
>>> ser = pd.Series(pd.to_timedelta([1, 2, 3], unit='d'))
812+
>>> ser = pd.Series(pd.to_timedelta([1, 2, 3], unit='D'))
813813
>>> ser
814814
0 1 days
815815
1 2 days

pandas/core/indexes/accessors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ def to_pytimedelta(self) -> np.ndarray:
459459
460460
Examples
461461
--------
462-
>>> s = pd.Series(pd.to_timedelta(np.arange(5), unit="d"))
462+
>>> s = pd.Series(pd.to_timedelta(np.arange(5), unit="D"))
463463
>>> s
464464
0 0 days
465465
1 1 days

pandas/core/tools/timedeltas.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def to_timedelta(
170170
TimedeltaIndex(['0 days 00:00:00', '0 days 00:00:01', '0 days 00:00:02',
171171
'0 days 00:00:03', '0 days 00:00:04'],
172172
dtype='timedelta64[ns]', freq=None)
173-
>>> pd.to_timedelta(np.arange(5), unit="d")
173+
>>> pd.to_timedelta(np.arange(5), unit="D")
174174
TimedeltaIndex(['0 days', '1 days', '2 days', '3 days', '4 days'],
175175
dtype='timedelta64[ns]', freq=None)
176176
"""

pandas/tests/frame/indexing/test_mask.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,15 @@ def test_mask_stringdtype(frame_or_series):
122122

123123
def test_mask_where_dtype_timedelta():
124124
# https://github.com/pandas-dev/pandas/issues/39548
125-
df = DataFrame([Timedelta(i, unit="d") for i in range(5)])
125+
df = DataFrame([Timedelta(i, unit="D") for i in range(5)])
126126

127127
expected = DataFrame(np.full(5, np.nan, dtype="timedelta64[ns]"))
128128
tm.assert_frame_equal(df.mask(df.notna()), expected)
129129

130130
expected = DataFrame(
131131
[np.nan, np.nan, np.nan, Timedelta("3 day"), Timedelta("4 day")]
132132
)
133-
tm.assert_frame_equal(df.where(df > Timedelta(2, unit="d")), expected)
133+
tm.assert_frame_equal(df.where(df > Timedelta(2, unit="D")), expected)
134134

135135

136136
def test_mask_return_dtype():

0 commit comments

Comments
 (0)