diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index eaa2b860e6a58c..d01a5fb4f8b433 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -407,6 +407,7 @@ or ``matplotlib.Axes.plot``. See :ref:`plotting.formatters` for more. - 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`) +- Passing ``datetime64`` data to :class:`TimedeltaIndex` or ``timedelta64`` data to ``DatetimeIndex`` now raises ``TypeError`` (:issue:`23539`, :issue:`23937`) - A tuple passed to :meth:`DataFrame.groupby` is now exclusively treated as a single key (:issue:`18314`) - Removed :meth:`Series.from_array` (:issue:`18258`) - Removed :meth:`DataFrame.from_items` (:issue:`18458`) diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 71420e6e58090c..fc3899bc615557 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -2102,14 +2102,8 @@ def maybe_convert_dtype(data, copy): # with integer dtypes. See discussion in GH#23675 elif is_timedelta64_dtype(data): - warnings.warn( - "Passing timedelta64-dtype data is deprecated, will " - "raise a TypeError in a future version", - FutureWarning, - stacklevel=5, - ) - data = data.view(_NS_DTYPE) - + # GH#29794 enforcing deprecation introduced in GH#23539 + raise TypeError(f"dtype {data.dtype} cannot be converted to datetime64[ns]") elif is_period_dtype(data): # Note: without explicitly raising here, PeriodIndex # test_setops.test_join_does_not_recur fails diff --git a/pandas/core/arrays/timedeltas.py b/pandas/core/arrays/timedeltas.py index bacd0b9699e93e..892d1bc281ca95 100644 --- a/pandas/core/arrays/timedeltas.py +++ b/pandas/core/arrays/timedeltas.py @@ -20,8 +20,6 @@ from pandas.core.dtypes.common import ( _NS_DTYPE, _TD_DTYPE, - ensure_int64, - is_datetime64_dtype, is_dtype_equal, is_float_dtype, is_integer_dtype, @@ -1056,18 +1054,8 @@ def sequence_to_td64ns(data, copy=False, unit="ns", errors="raise"): data = data.astype(_TD_DTYPE) copy = False - elif is_datetime64_dtype(data): - # GH#23539 - warnings.warn( - "Passing datetime64-dtype data to TimedeltaIndex is " - "deprecated, will raise a TypeError in a future " - "version", - FutureWarning, - stacklevel=4, - ) - data = ensure_int64(data).view(_TD_DTYPE) - else: + # This includes datetime64-dtype, see GH#23539, GH#29794 raise TypeError( "dtype {dtype} cannot be converted to timedelta64[ns]".format( dtype=data.dtype diff --git a/pandas/tests/arithmetic/test_timedelta64.py b/pandas/tests/arithmetic/test_timedelta64.py index d45daf9ab84336..79270c2ea2cabb 100644 --- a/pandas/tests/arithmetic/test_timedelta64.py +++ b/pandas/tests/arithmetic/test_timedelta64.py @@ -577,15 +577,23 @@ def test_timedelta_ops_with_missing_values(self): # setup s1 = pd.to_timedelta(Series(["00:00:01"])) s2 = pd.to_timedelta(Series(["00:00:02"])) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - # Passing datetime64-dtype data to TimedeltaIndex is deprecated - sn = pd.to_timedelta(Series([pd.NaT])) + + msg = r"dtype datetime64\[ns\] cannot be converted to timedelta64\[ns\]" + with pytest.raises(TypeError, match=msg): + # Passing datetime64-dtype data to TimedeltaIndex is no longer + # supported GH#29794 + pd.to_timedelta(Series([pd.NaT])) + + sn = pd.to_timedelta(Series([pd.NaT], dtype="m8[ns]")) df1 = pd.DataFrame(["00:00:01"]).apply(pd.to_timedelta) df2 = pd.DataFrame(["00:00:02"]).apply(pd.to_timedelta) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - # Passing datetime64-dtype data to TimedeltaIndex is deprecated - dfn = pd.DataFrame([pd.NaT]).apply(pd.to_timedelta) + with pytest.raises(TypeError, match=msg): + # Passing datetime64-dtype data to TimedeltaIndex is no longer + # supported GH#29794 + pd.DataFrame([pd.NaT]).apply(pd.to_timedelta) + + dfn = pd.DataFrame([pd.NaT.value]).apply(pd.to_timedelta) scalar1 = pd.to_timedelta("00:00:01") scalar2 = pd.to_timedelta("00:00:02") diff --git a/pandas/tests/indexes/datetimes/test_construction.py b/pandas/tests/indexes/datetimes/test_construction.py index 88bc11c588673a..4ef1cbd5af9587 100644 --- a/pandas/tests/indexes/datetimes/test_construction.py +++ b/pandas/tests/indexes/datetimes/test_construction.py @@ -70,28 +70,21 @@ def test_dti_with_period_data_raises(self): with pytest.raises(TypeError, match="PeriodDtype data is invalid"): to_datetime(period_array(data)) - def test_dti_with_timedelta64_data_deprecation(self): - # GH#23675 + def test_dti_with_timedelta64_data_raises(self): + # GH#23675 deprecated, enforrced in GH#29794 data = np.array([0], dtype="m8[ns]") - with tm.assert_produces_warning(FutureWarning): - result = DatetimeIndex(data) - - assert result[0] == Timestamp("1970-01-01") - - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result = to_datetime(data) - - assert result[0] == Timestamp("1970-01-01") - - with tm.assert_produces_warning(FutureWarning): - result = DatetimeIndex(pd.TimedeltaIndex(data)) + msg = r"timedelta64\[ns\] cannot be converted to datetime64" + with pytest.raises(TypeError, match=msg): + DatetimeIndex(data) - assert result[0] == Timestamp("1970-01-01") + with pytest.raises(TypeError, match=msg): + to_datetime(data) - with tm.assert_produces_warning(FutureWarning, check_stacklevel=False): - result = to_datetime(pd.TimedeltaIndex(data)) + with pytest.raises(TypeError, match=msg): + DatetimeIndex(pd.TimedeltaIndex(data)) - assert result[0] == Timestamp("1970-01-01") + with pytest.raises(TypeError, match=msg): + to_datetime(pd.TimedeltaIndex(data)) def test_construction_caching(self): diff --git a/pandas/tests/indexes/timedeltas/test_construction.py b/pandas/tests/indexes/timedeltas/test_construction.py index 2e00d558958e17..3cf86bea1d6de4 100644 --- a/pandas/tests/indexes/timedeltas/test_construction.py +++ b/pandas/tests/indexes/timedeltas/test_construction.py @@ -60,17 +60,17 @@ def test_infer_from_tdi_mismatch(self): def test_dt64_data_invalid(self): # GH#23539 # passing tz-aware DatetimeIndex raises, naive or ndarray[datetime64] - # does not yet, but will in the future + # raise as of GH#29794 dti = pd.date_range("2016-01-01", periods=3) msg = "cannot be converted to timedelta64" with pytest.raises(TypeError, match=msg): TimedeltaIndex(dti.tz_localize("Europe/Brussels")) - with tm.assert_produces_warning(FutureWarning): + with pytest.raises(TypeError, match=msg): TimedeltaIndex(dti) - with tm.assert_produces_warning(FutureWarning): + with pytest.raises(TypeError, match=msg): TimedeltaIndex(np.asarray(dti)) def test_float64_ns_rounded(self):