Skip to content

Commit 2fbe070

Browse files
author
Chris Bertinato
committed
DEPR: Deprecate box kwarg for to_timedelta and to_datetime
1 parent 3086e0a commit 2fbe070

File tree

5 files changed

+39
-0
lines changed

5 files changed

+39
-0
lines changed

doc/source/whatsnew/v0.24.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,7 @@ Deprecations
11521152
- Passing a string alias like ``'datetime64[ns, UTC]'`` as the ``unit`` parameter to :class:`DatetimeTZDtype` is deprecated. Use :class:`DatetimeTZDtype.construct_from_string` instead (:issue:`23990`).
11531153
- In :meth:`Series.where` with Categorical data, providing an ``other`` that is not present in the categories is deprecated. Convert the categorical to a different dtype or add the ``other`` to the categories first (:issue:`24077`).
11541154
- :meth:`Series.clip_lower`, :meth:`Series.clip_upper`, :meth:`DataFrame.clip_lower` and :meth:`DataFrame.clip_upper` are deprecated and will be removed in a future version. Use ``Series.clip(lower=threshold)``, ``Series.clip(upper=threshold)`` and the equivalent ``DataFrame`` methods (:issue:`24203`)
1155+
- The functions :func:`pandas.to_datetime` and :func:`pandas.to_timedelta` have deprecated the ``box`` keyword. Use :attr:`Series.values` and :meth:`Timestamp.to_datetime64`/:meth:`Timedelta.to_timedelta64` instead to get an ndarray of values or ``numpy.timestamp64``/``numpy.timedelta64``, respectively (:issue:`24416`).
11551156

11561157
.. _whatsnew_0240.deprecations.datetimelike_int_ops:
11571158

pandas/core/tools/datetimes.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
from pandas import compat
2121
from pandas.core import algorithms
2222

23+
from pandas.util._decorators import deprecate_kwarg
24+
2325

2426
def _guess_datetime_format_for_array(arr, **kwargs):
2527
# Try to guess the format based on the first non-NaN element
@@ -388,6 +390,7 @@ def _adjust_to_origin(arg, origin, unit):
388390
return arg
389391

390392

393+
@deprecate_kwarg(old_arg_name='box', new_arg_name=None)
391394
def to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False,
392395
utc=None, box=True, format=None, exact=True,
393396
unit=None, infer_datetime_format=False, origin='unix',
@@ -434,6 +437,12 @@ def to_datetime(arg, errors='raise', dayfirst=False, yearfirst=False,
434437
435438
- If True returns a DatetimeIndex or Index-like object
436439
- If False returns ndarray of values.
440+
441+
.. deprecated:: 0.24.0
442+
Use :attr:`Series.values` or :meth:`Timestamp.to_datetime64`
443+
instead to get an ndarray of values or numpy.datetime64,
444+
respectively.
445+
437446
format : string, default None
438447
strftime to parse time, eg "%d/%m/%Y", note that "%f" will parse
439448
all the way up to nanoseconds.

pandas/core/tools/timedeltas.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
import pandas as pd
1515
from pandas.core.arrays.timedeltas import sequence_to_td64ns
1616

17+
from pandas.util._decorators import deprecate_kwarg
1718

19+
20+
@deprecate_kwarg(old_arg_name='box', new_arg_name=None)
1821
def to_timedelta(arg, unit='ns', box=True, errors='raise'):
1922
"""
2023
Convert argument to timedelta.
@@ -40,6 +43,12 @@ def to_timedelta(arg, unit='ns', box=True, errors='raise'):
4043
- If True returns a Timedelta/TimedeltaIndex of the results.
4144
- If False returns a numpy.timedelta64 or numpy.darray of
4245
values of dtype timedelta64[ns].
46+
47+
.. deprecated:: 0.24.0
48+
Use :attr:`Series.values` or :meth:`Timedelta.to_timedelta64`
49+
instead to get an ndarray of values or numpy.timedelta64,
50+
respectively.
51+
4352
errors : {'ignore', 'raise', 'coerce'}, default 'raise'
4453
- If 'raise', then invalid parsing will raise an exception.
4554
- If 'coerce', then invalid parsing will be set as NaT.

pandas/tests/indexes/datetimes/test_tools.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,16 @@ def test_timestamp_utc_true(self, ts, expected):
665665
result = to_datetime(ts, utc=True)
666666
assert result == expected
667667

668+
def test_to_datetime_box_deprecated(self):
669+
expected = np.datetime64('2018-09-09')
670+
671+
# Deprecated - see GH24416
672+
with tm.assert_produces_warning(FutureWarning):
673+
pd.to_datetime(expected, box=False)
674+
675+
result = pd.to_datetime(expected).to_datetime64()
676+
assert result == expected
677+
668678

669679
class TestToDatetimeUnit(object):
670680
@pytest.mark.parametrize('cache', [True, False])

pandas/tests/indexes/timedeltas/test_tools.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,13 @@ def test_to_timedelta_on_missing_values(self):
172172

173173
actual = pd.to_timedelta(pd.NaT)
174174
assert actual.value == timedelta_NaT.astype('int64')
175+
176+
def test_to_timedelta_box_deprecated(self):
177+
result = np.timedelta64(0, 'ns')
178+
179+
# Deprecated - see GH24416
180+
with tm.assert_produces_warning(FutureWarning):
181+
to_timedelta(0, box=False)
182+
183+
expected = to_timedelta(0).to_timedelta64()
184+
assert result == expected

0 commit comments

Comments
 (0)