From 4573002df01bc18f98320dce24d74a263bc970e6 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 22 Nov 2019 09:58:01 -0800 Subject: [PATCH 1/2] DEPR: Timedelta.__rfloordiv__(int_dtype) --- doc/source/whatsnew/v1.0.0.rst | 1 + pandas/_libs/tslibs/timedeltas.pyx | 14 ++------------ pandas/tests/scalar/timedelta/test_arithmetic.py | 8 +++++--- pandas/tests/scalar/timedelta/test_timedelta.py | 12 +++--------- 4 files changed, 11 insertions(+), 24 deletions(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index e302e209b56a1..3bfc454bac3e4 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -311,6 +311,7 @@ or ``matplotlib.Axes.plot``. See :ref:`plotting.formatters` for more. **Other removals** +- Floordiv of integer-dtyped array by :class:`Timedelta` now raises ``TypeError`` (:issue:`21036`) - Removed the previously deprecated :meth:`Series.get_value`, :meth:`Series.set_value`, :meth:`DataFrame.get_value`, :meth:`DataFrame.set_value` (:issue:`17739`) - Changed the the default value of `inplace` in :meth:`DataFrame.set_index` and :meth:`Series.set_axis`. It now defaults to False (:issue:`27600`) - Removed support for nested renaming in :meth:`DataFrame.aggregate`, :meth:`Series.aggregate`, :meth:`DataFrameGroupBy.aggregate`, :meth:`SeriesGroupBy.aggregate`, :meth:`Rolling.aggregate` (:issue:`18529`) diff --git a/pandas/_libs/tslibs/timedeltas.pyx b/pandas/_libs/tslibs/timedeltas.pyx index 8e5b719749857..48a2a05011ab5 100644 --- a/pandas/_libs/tslibs/timedeltas.pyx +++ b/pandas/_libs/tslibs/timedeltas.pyx @@ -1509,18 +1509,8 @@ class Timedelta(_Timedelta): if other.dtype.kind == 'm': # also timedelta-like return _broadcast_floordiv_td64(self.value, other, _rfloordiv) - elif other.dtype.kind == 'i': - # Backwards compatibility - # GH-19761 - msg = textwrap.dedent("""\ - Floor division between integer array and Timedelta is - deprecated. Use 'array // timedelta.value' instead. - If you want to obtain epochs from an array of timestamps, - you can rather use - '(array - pd.Timestamp("1970-01-01")) // pd.Timedelta("1s")'. - """) - warnings.warn(msg, FutureWarning) - return other // self.value + + # Includes integer array // Timedelta, deprecated in GH#19761 raise TypeError(f'Invalid dtype {other.dtype} for __floordiv__') elif is_float_object(other) and util.is_nan(other): diff --git a/pandas/tests/scalar/timedelta/test_arithmetic.py b/pandas/tests/scalar/timedelta/test_arithmetic.py index 2ba55b22a7c54..f67b01122999a 100644 --- a/pandas/tests/scalar/timedelta/test_arithmetic.py +++ b/pandas/tests/scalar/timedelta/test_arithmetic.py @@ -463,8 +463,8 @@ def test_td_rfloordiv_numeric_scalar(self): td.__rfloordiv__(np.float64(2.0)) with pytest.raises(TypeError): td.__rfloordiv__(np.uint8(9)) - with tm.assert_produces_warning(FutureWarning): - # GH-19761: Change to TypeError. + with pytest.raises(TypeError, match="Invalid dtype"): + # deprecated GH#19761, enforced GH#???? td.__rfloordiv__(np.int32(2.0)) def test_td_rfloordiv_timedeltalike_array(self): @@ -490,7 +490,9 @@ def test_td_rfloordiv_numeric_series(self): ser = pd.Series([1], dtype=np.int64) res = td.__rfloordiv__(ser) assert res is NotImplemented - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): + + with pytest.raises(TypeError, match="Invalid dtype"): + # Deprecated GH#19761, enforced GH#???? # TODO: GH-19761. Change to TypeError. ser // td diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index 9bb6c991a930a..b341c9f1d2c09 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -21,17 +21,11 @@ def test_arithmetic_overflow(self): Timestamp("1700-01-01") + timedelta(days=13 * 19999) def test_array_timedelta_floordiv(self): - # https://github.com/pandas-dev/pandas/issues/19761 + # deprected GH#19761, enforced GH#????? ints = pd.date_range("2012-10-08", periods=4, freq="D").view("i8") - msg = r"Use 'array // timedelta.value'" - with tm.assert_produces_warning(FutureWarning) as m: - result = ints // Timedelta(1, unit="s") - assert msg in str(m[0].message) - expected = np.array( - [1349654400, 1349740800, 1349827200, 1349913600], dtype="i8" - ) - tm.assert_numpy_array_equal(result, expected) + with pytest.raises(TypeError, match="Invalid dtype"): + ints // Timedelta(1, unit="s") def test_ops_error_str(self): # GH 13624 From 0a4d05bd74d167a15cc740a98f0237f96aede4fd Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Fri, 22 Nov 2019 09:59:30 -0800 Subject: [PATCH 2/2] update gh references --- pandas/tests/scalar/timedelta/test_arithmetic.py | 4 ++-- pandas/tests/scalar/timedelta/test_timedelta.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/tests/scalar/timedelta/test_arithmetic.py b/pandas/tests/scalar/timedelta/test_arithmetic.py index f67b01122999a..57e0b1d743984 100644 --- a/pandas/tests/scalar/timedelta/test_arithmetic.py +++ b/pandas/tests/scalar/timedelta/test_arithmetic.py @@ -464,7 +464,7 @@ def test_td_rfloordiv_numeric_scalar(self): with pytest.raises(TypeError): td.__rfloordiv__(np.uint8(9)) with pytest.raises(TypeError, match="Invalid dtype"): - # deprecated GH#19761, enforced GH#???? + # deprecated GH#19761, enforced GH#29797 td.__rfloordiv__(np.int32(2.0)) def test_td_rfloordiv_timedeltalike_array(self): @@ -492,7 +492,7 @@ def test_td_rfloordiv_numeric_series(self): assert res is NotImplemented with pytest.raises(TypeError, match="Invalid dtype"): - # Deprecated GH#19761, enforced GH#???? + # Deprecated GH#19761, enforced GH#29797 # TODO: GH-19761. Change to TypeError. ser // td diff --git a/pandas/tests/scalar/timedelta/test_timedelta.py b/pandas/tests/scalar/timedelta/test_timedelta.py index b341c9f1d2c09..d4881ff0e1747 100644 --- a/pandas/tests/scalar/timedelta/test_timedelta.py +++ b/pandas/tests/scalar/timedelta/test_timedelta.py @@ -21,7 +21,7 @@ def test_arithmetic_overflow(self): Timestamp("1700-01-01") + timedelta(days=13 * 19999) def test_array_timedelta_floordiv(self): - # deprected GH#19761, enforced GH#????? + # deprected GH#19761, enforced GH#29797 ints = pd.date_range("2012-10-08", periods=4, freq="D").view("i8") with pytest.raises(TypeError, match="Invalid dtype"):