Skip to content

Commit 768346b

Browse files
authored
DEPR: disallow unit-less dt64 dtype in astype (#49391)
1 parent cc2aa48 commit 768346b

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ Removal of prior version deprecations/changes
182182
- Removed deprecated argument ``null_color`` in :meth:`.Styler.highlight_null` (:issue:`49397`)
183183
- Enforced deprecation disallowing passing a timezone-aware :class:`Timestamp` and ``dtype="datetime64[ns]"`` to :class:`Series` or :class:`DataFrame` constructors (:issue:`41555`)
184184
- Enforced deprecation disallowing passing a sequence of timezone-aware values and ``dtype="datetime64[ns]"`` to to :class:`Series` or :class:`DataFrame` constructors (:issue:`41555`)
185+
- Enforced deprecation disallowing unit-less "datetime64" dtype in :meth:`Series.astype` and :meth:`DataFrame.astype` (:issue:`47844`)
185186
- 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`)
186187
- 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`)
187188
- Removed Date parser functions :func:`~pandas.io.date_converters.parse_date_time`,

pandas/core/arrays/datetimes.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -683,15 +683,10 @@ def astype(self, dtype, copy: bool = True):
683683
and dtype != self.dtype
684684
and is_unitless(dtype)
685685
):
686-
# TODO(2.0): just fall through to dtl.DatetimeLikeArrayMixin.astype
687-
warnings.warn(
688-
"Passing unit-less datetime64 dtype to .astype is deprecated "
689-
"and will raise in a future version. Pass 'datetime64[ns]' instead",
690-
FutureWarning,
691-
stacklevel=find_stack_level(),
686+
raise TypeError(
687+
"Casting to unit-less dtype 'datetime64' is not supported. "
688+
"Pass e.g. 'datetime64[ns]' instead."
692689
)
693-
# unit conversion e.g. datetime64[s]
694-
return self._ndarray.astype(dtype)
695690

696691
elif is_period_dtype(dtype):
697692
return self.to_period(freq=dtype.freq)

pandas/tests/series/methods/test_astype.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,20 @@
3030

3131

3232
class TestAstypeAPI:
33-
def test_astype_unitless_dt64_deprecated(self):
33+
def test_astype_unitless_dt64_raises(self):
3434
# GH#47844
3535
ser = Series(["1970-01-01", "1970-01-01", "1970-01-01"], dtype="datetime64[ns]")
36+
df = ser.to_frame()
3637

37-
msg = "Passing unit-less datetime64 dtype to .astype is deprecated and "
38-
with tm.assert_produces_warning(FutureWarning, match=msg):
39-
res = ser.astype(np.datetime64)
40-
tm.assert_series_equal(ser, res)
41-
42-
with tm.assert_produces_warning(FutureWarning, match=msg):
43-
res = ser.astype("datetime64")
44-
tm.assert_series_equal(ser, res)
38+
msg = "Casting to unit-less dtype 'datetime64' is not supported"
39+
with pytest.raises(TypeError, match=msg):
40+
ser.astype(np.datetime64)
41+
with pytest.raises(TypeError, match=msg):
42+
df.astype(np.datetime64)
43+
with pytest.raises(TypeError, match=msg):
44+
ser.astype("datetime64")
45+
with pytest.raises(TypeError, match=msg):
46+
df.astype("datetime64")
4547

4648
def test_arg_for_errors_in_astype(self):
4749
# see GH#14878

0 commit comments

Comments
 (0)