diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 1f245b585df48..2cad922a71a3c 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -175,6 +175,7 @@ Removal of prior version deprecations/changes - Removed deprecated :meth:`DatetimeIndex.to_perioddelta`, Use ``dtindex - dtindex.to_period(freq).to_timestamp()`` instead (:issue:`34853`) - Enforced deprecation disallowing passing a timezone-aware :class:`Timestamp` and ``dtype="datetime64[ns]"`` to :class:`Series` or :class:`DataFrame` constructors (:issue:`41555`) - Enforced deprecation disallowing passing a sequence of timezone-aware values and ``dtype="datetime64[ns]"`` to to :class:`Series` or :class:`DataFrame` constructors (:issue:`41555`) +- Enforced deprecation disallowing unit-less "datetime64" dtype in :meth:`Series.astype` and :meth:`DataFrame.astype` (:issue:`47844`) - Enforced deprecation disallowing using ``.astype`` to convert a ``datetime64[ns]`` :class:`Series`, :class:`DataFrame`, or :class:`DatetimeIndex` to timezone-aware dtype, use ``obj.tz_localize`` or ``ser.dt.tz_localize`` instead (:issue:`39258`) - Enforced deprecation disallowing using ``.astype`` to convert a timezone-aware :class:`Series`, :class:`DataFrame`, or :class:`DatetimeIndex` to timezone-naive ``datetime64[ns]`` dtype, use ``obj.tz_localize(None)`` or ``obj.tz_convert("UTC").tz_localize(None)`` instead (:issue:`39258`) - Removed Date parser functions :func:`~pandas.io.date_converters.parse_date_time`, diff --git a/pandas/core/arrays/datetimes.py b/pandas/core/arrays/datetimes.py index 8fc18691fb17e..ca54ab163ab64 100644 --- a/pandas/core/arrays/datetimes.py +++ b/pandas/core/arrays/datetimes.py @@ -683,15 +683,10 @@ def astype(self, dtype, copy: bool = True): and dtype != self.dtype and is_unitless(dtype) ): - # TODO(2.0): just fall through to dtl.DatetimeLikeArrayMixin.astype - warnings.warn( - "Passing unit-less datetime64 dtype to .astype is deprecated " - "and will raise in a future version. Pass 'datetime64[ns]' instead", - FutureWarning, - stacklevel=find_stack_level(), + raise TypeError( + "Casting to unit-less dtype 'datetime64' is not supported. " + "Pass e.g. 'datetime64[ns]' instead." ) - # unit conversion e.g. datetime64[s] - return self._ndarray.astype(dtype) elif is_period_dtype(dtype): return self.to_period(freq=dtype.freq) diff --git a/pandas/tests/series/methods/test_astype.py b/pandas/tests/series/methods/test_astype.py index 9b57f0f634a6c..1423581555ee6 100644 --- a/pandas/tests/series/methods/test_astype.py +++ b/pandas/tests/series/methods/test_astype.py @@ -30,18 +30,20 @@ class TestAstypeAPI: - def test_astype_unitless_dt64_deprecated(self): + def test_astype_unitless_dt64_raises(self): # GH#47844 ser = Series(["1970-01-01", "1970-01-01", "1970-01-01"], dtype="datetime64[ns]") + df = ser.to_frame() - msg = "Passing unit-less datetime64 dtype to .astype is deprecated and " - with tm.assert_produces_warning(FutureWarning, match=msg): - res = ser.astype(np.datetime64) - tm.assert_series_equal(ser, res) - - with tm.assert_produces_warning(FutureWarning, match=msg): - res = ser.astype("datetime64") - tm.assert_series_equal(ser, res) + msg = "Casting to unit-less dtype 'datetime64' is not supported" + with pytest.raises(TypeError, match=msg): + ser.astype(np.datetime64) + with pytest.raises(TypeError, match=msg): + df.astype(np.datetime64) + with pytest.raises(TypeError, match=msg): + ser.astype("datetime64") + with pytest.raises(TypeError, match=msg): + df.astype("datetime64") def test_arg_for_errors_in_astype(self): # see GH#14878