Skip to content

Commit 734be53

Browse files
committed
Warn on ndarray[int] // timedelta
Closes pandas-dev#19761. ```python In [2]: pd.DatetimeIndex(['1931', '1970', '2017']).view('i8') // pd.Timedelta(1, unit='s') pandas-dev/bin/ipython:1: FutureWarning: Floor division between integer array and Timedelta is deprecated. Use 'array // timedelta.value' instead. Out[2]: array([-1230768000, 0, 1483228800]) ```
1 parent a5c02d5 commit 734be53

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

doc/source/timeseries.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ We convert the ``DatetimeIndex`` to an ``int64`` array, then divide by the conve
308308

309309
.. ipython:: python
310310
311-
stamps.view('int64') // pd.Timedelta(1, unit='s')
311+
stamps.view('int64') // pd.Timedelta(1, unit='s').value
312312
313313
.. _timeseries.origin:
314314

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,7 @@ Deprecations
10041004
of the ``Series`` and ``Index`` classes have been deprecated and will be
10051005
removed in a future version (:issue:`20419`).
10061006
- ``DatetimeIndex.offset`` is deprecated. Use ``DatetimeIndex.freq`` instead (:issue:`20716`)
1007+
- Floor division between an integer ndarray and a :class:`Timedelta` is deprecated. Divide by :attr:`Timedelta.value` instead (:issue:`19761`)
10071008
- Setting ``PeriodIndex.freq`` (which was not guaranteed to work correctly) is deprecated. Use :meth:`PeriodIndex.asfreq` instead (:issue:`20678`)
10081009
- ``Index.get_duplicates()`` is deprecated and will be removed in a future version (:issue:`20239`)
10091010
- The previous default behavior of negative indices in ``Categorical.take`` is deprecated. In a future version it will change from meaning missing values to meaning positional indices from the right. The future behavior is consistent with :meth:`Series.take` (:issue:`20664`).

pandas/_libs/tslibs/timedeltas.pyx

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# -*- coding: utf-8 -*-
22
# cython: profile=False
33
import collections
4+
import textwrap
5+
import warnings
46

57
import sys
68
cdef bint PY3 = (sys.version_info[0] >= 3)
@@ -1188,6 +1190,15 @@ class Timedelta(_Timedelta):
11881190
if other.dtype.kind == 'm':
11891191
# also timedelta-like
11901192
return _broadcast_floordiv_td64(self.value, other, _rfloordiv)
1193+
elif other.dtype.kind == 'i':
1194+
# Backwards compatibility
1195+
# GH-19761
1196+
msg = textwrap.dedent("""\
1197+
Floor division between integer array and Timedelta is
1198+
deprecated. Use 'array // timedelta.value' instead.
1199+
""")
1200+
warnings.warn(msg, FutureWarning)
1201+
return other // self.value
11911202
raise TypeError('Invalid dtype {dtype} for '
11921203
'{op}'.format(dtype=other.dtype,
11931204
op='__floordiv__'))

pandas/tests/scalar/timedelta/test_timedelta.py

+12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ def test_arithmetic_overflow(self):
2121
with pytest.raises(OverflowError):
2222
pd.Timestamp('1700-01-01') + timedelta(days=13 * 19999)
2323

24+
def test_array_timedelta_floordiv(self):
25+
# https://github.com/pandas-dev/pandas/issues/19761
26+
ints = pd.date_range('2012-10-08', periods=4, freq='D').view('i8')
27+
msg = r"Use 'array // timedelta.value'"
28+
with tm.assert_produces_warning(FutureWarning) as m:
29+
result = ints // pd.Timedelta(1, unit='s')
30+
31+
assert msg in str(m[0].message)
32+
expected = np.array([1349654400, 1349740800, 1349827200, 1349913600],
33+
dtype='i8')
34+
tm.assert_numpy_array_equal(result, expected)
35+
2436
def test_ops_error_str(self):
2537
# GH 13624
2638
td = Timedelta('1 day')

0 commit comments

Comments
 (0)