Skip to content

API: Series.astype(dt64_unsupported) raise #49482

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

Closed
Closed
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 @@ -146,6 +146,7 @@ Other API changes
- :meth:`Series.unique` with dtype "timedelta64[ns]" or "datetime64[ns]" now returns :class:`TimedeltaArray` or :class:`DatetimeArray` instead of ``numpy.ndarray`` (:issue:`49176`)
- Passing a sequence containing ``datetime`` objects and ``date`` objects to :class:`Series` constructor will return with ``object`` dtype instead of ``datetime64[ns]`` dtype, consistent with :class:`Index` behavior (:issue:`49341`)
- Passing strings that cannot be parsed as datetimes to :class:`Series` or :class:`DataFrame` with ``dtype="datetime64[ns]"`` will raise instead of silently ignoring the keyword and returning ``object`` dtype (:issue:`24435`)
- :meth:`Series.astype` and :meth:`DataFrame.astype` casting to ``datetime64`` dtypes other than "s", "ms", "us", and "ns" now raise instead of silently returning ``datetime64[ns]`` (:issue:`49482`)
-

.. ---------------------------------------------------------------------------
Expand Down
15 changes: 0 additions & 15 deletions pandas/core/dtypes/astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from pandas._libs.tslibs import (
get_unit_from_dtype,
is_supported_unit,
is_unitless,
)
from pandas._libs.tslibs.timedeltas import array_to_timedelta64
from pandas._typing import (
Expand Down Expand Up @@ -292,20 +291,6 @@ def astype_array_safe(
# Ensure we don't end up with a PandasArray
dtype = dtype.numpy_dtype

if (
is_datetime64_dtype(values.dtype)
# need to do np.dtype check instead of is_datetime64_dtype
# otherwise pyright complains
and isinstance(dtype, np.dtype)
and dtype.kind == "M"
and not is_unitless(dtype)
and not is_dtype_equal(dtype, values.dtype)
and not is_supported_unit(get_unit_from_dtype(dtype))
):
# Supported units we handle in DatetimeArray.astype; but that raises
# on non-supported units, so we handle that here.
return np.asarray(values).astype(dtype)

try:
new_values = astype_array(values, dtype, copy=copy)
except (ValueError, TypeError):
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/frame/constructors/test_from_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ def test_from_records_with_datetimes(self):
dtypes = [("EXPIRY", "<M8[m]")]
recarray = np.core.records.fromarrays(arrdata, dtype=dtypes)
result = DataFrame.from_records(recarray)
expected["EXPIRY"] = expected["EXPIRY"].astype("M8[m]")
# the construction casts M8[m] to the nearest supported dtype: M8[s]
expected["EXPIRY"] = expected["EXPIRY"].astype("M8[s]")
tm.assert_frame_equal(result, expected)

def test_from_records_sequencelike(self):
Expand Down
54 changes: 29 additions & 25 deletions pandas/tests/frame/methods/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,14 +418,28 @@ def test_astype_to_datetime_unit(self, unit):
idx = pd.Index(ser)
dta = ser._values

if unit not in ["ns", "us", "ms", "s"]:
# We disallow; pre-2.0 we ignored the dtype and returned ns
msg = rf"Cannot cast DatetimeArray to dtype datetime64\[{unit}\]"
msg2 = rf"Cannot cast DatetimeIndex to dtype datetime64\[{unit}\]"
with pytest.raises(TypeError, match=msg):
df.astype(dtype)

with pytest.raises(TypeError, match=msg):
ser.astype(dtype)

with pytest.raises(TypeError, match=msg2):
idx.astype(dtype)

with pytest.raises(TypeError, match=msg):
dta.astype(dtype)

return

# GH#48928
exp_dtype = dtype
result = df.astype(dtype)

if unit in ["ns", "us", "ms", "s"]:
# GH#48928
exp_dtype = dtype
else:
# we use the nearest supported dtype (i.e. M8[s])
exp_dtype = "M8[s]"
# TODO(2.0): once DataFrame constructor doesn't cast ndarray inputs.
# can simplify this
exp_values = arr.astype(exp_dtype)
Expand All @@ -437,32 +451,22 @@ def test_astype_to_datetime_unit(self, unit):

tm.assert_frame_equal(result, exp_df)

# TODO(2.0): make Series/DataFrame raise like Index and DTA?
res_ser = ser.astype(dtype)
exp_ser = exp_df.iloc[:, 0]
assert exp_ser.dtype == exp_dtype
tm.assert_series_equal(res_ser, exp_ser)

if unit in ["ns", "us", "ms", "s"]:
exp_dta = exp_ser._values
exp_dta = exp_ser._values

res_index = idx.astype(dtype)
# TODO(2.0): should be able to just call pd.Index(exp_ser)
exp_index = pd.DatetimeIndex._simple_new(exp_dta, name=idx.name)
assert exp_index.dtype == exp_dtype
tm.assert_index_equal(res_index, exp_index)
res_index = idx.astype(dtype)
# TODO(2.0): should be able to just call pd.Index(exp_ser)
exp_index = pd.DatetimeIndex._simple_new(exp_dta, name=idx.name)
assert exp_index.dtype == exp_dtype
tm.assert_index_equal(res_index, exp_index)

res_dta = dta.astype(dtype)
assert exp_dta.dtype == exp_dtype
tm.assert_extension_array_equal(res_dta, exp_dta)
else:
msg = rf"Cannot cast DatetimeIndex to dtype datetime64\[{unit}\]"
with pytest.raises(TypeError, match=msg):
idx.astype(dtype)

msg = rf"Cannot cast DatetimeArray to dtype datetime64\[{unit}\]"
with pytest.raises(TypeError, match=msg):
dta.astype(dtype)
res_dta = dta.astype(dtype)
assert exp_dta.dtype == exp_dtype
tm.assert_extension_array_equal(res_dta, exp_dta)

@pytest.mark.parametrize("unit", ["ns"])
def test_astype_to_timedelta_unit_ns(self, unit):
Expand Down