Skip to content

DEPR: disallow unit-less dt64 dtype in astype #49391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
11 changes: 3 additions & 8 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 11 additions & 9 deletions pandas/tests/series/methods/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down