Skip to content

BUG: frame[object].astype(M8[unsupported]) not raising #50015

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
Dec 2, 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
11 changes: 5 additions & 6 deletions pandas/core/dtypes/astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,15 @@ def astype_nansafe(
elif is_object_dtype(arr.dtype):

# if we have a datetime/timedelta array of objects
# then coerce to a proper dtype and recall astype_nansafe
# then coerce to datetime64[ns] and use DatetimeArray.astype

if is_datetime64_dtype(dtype):
from pandas import to_datetime

return astype_nansafe(
to_datetime(arr.ravel()).values.reshape(arr.shape),
dtype,
copy=copy,
)
dti = to_datetime(arr.ravel())
dta = dti._data.reshape(arr.shape)
return dta.astype(dtype, copy=False)._ndarray

elif is_timedelta64_dtype(dtype):
# bc we know arr.dtype == object, this is equivalent to
# `np.asarray(to_timedelta(arr))`, but using a lower-level API that
Expand Down
8 changes: 7 additions & 1 deletion pandas/tests/dtypes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,13 @@ def test_astype_datetime64_bad_dtype_raises(from_type, to_type):

to_type = np.dtype(to_type)

with pytest.raises(TypeError, match="cannot astype"):
msg = "|".join(
[
"cannot astype a timedelta",
"cannot astype a datetimelike",
]
)
with pytest.raises(TypeError, match=msg):
astype_nansafe(arr, dtype=to_type)


Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/frame/methods/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,16 @@ def test_astype_column_metadata(self, dtype):
df = df.astype(dtype)
tm.assert_index_equal(df.columns, columns)

@pytest.mark.parametrize("unit", ["Y", "M", "W", "D", "h", "m"])
def test_astype_from_object_to_datetime_unit(self, unit):
vals = [
["2015-01-01", "2015-01-02", "2015-01-03"],
["2017-01-01", "2017-01-02", "2017-02-03"],
]
df = DataFrame(vals, dtype=object)
with pytest.raises(TypeError, match="Cannot cast"):
df.astype(f"M8[{unit}]")

@pytest.mark.parametrize("dtype", ["M8", "m8"])
@pytest.mark.parametrize("unit", ["ns", "us", "ms", "s", "h", "m", "D"])
def test_astype_from_datetimelike_to_object(self, dtype, unit):
Expand Down
33 changes: 14 additions & 19 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1955,19 +1955,11 @@ def test_constructor_datetimes_with_nulls(self, arr):

@pytest.mark.parametrize("order", ["K", "A", "C", "F"])
@pytest.mark.parametrize(
"dtype",
[
"datetime64[M]",
"datetime64[D]",
"datetime64[h]",
"datetime64[m]",
"datetime64[s]",
"datetime64[ms]",
"datetime64[us]",
"datetime64[ns]",
],
"unit",
["M", "D", "h", "m", "s", "ms", "us", "ns"],
)
def test_constructor_datetimes_non_ns(self, order, dtype):
def test_constructor_datetimes_non_ns(self, order, unit):
dtype = f"datetime64[{unit}]"
na = np.array(
[
["2015-01-01", "2015-01-02", "2015-01-03"],
Expand All @@ -1977,13 +1969,16 @@ def test_constructor_datetimes_non_ns(self, order, dtype):
order=order,
)
df = DataFrame(na)
expected = DataFrame(
[
["2015-01-01", "2015-01-02", "2015-01-03"],
["2017-01-01", "2017-01-02", "2017-02-03"],
]
)
expected = expected.astype(dtype=dtype)
expected = DataFrame(na.astype("M8[ns]"))
if unit in ["M", "D", "h", "m"]:
with pytest.raises(TypeError, match="Cannot cast"):
expected.astype(dtype)

# instead the constructor casts to the closest supported reso, i.e. "s"
expected = expected.astype("datetime64[s]")
else:
expected = expected.astype(dtype=dtype)

tm.assert_frame_equal(df, expected)

@pytest.mark.parametrize("order", ["K", "A", "C", "F"])
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/io/xml/test_xml_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ def test_dtypes_with_names(parser):
df_result = read_xml(
xml_dates,
names=["Col1", "Col2", "Col3", "Col4"],
dtype={"Col2": "string", "Col3": "Int64", "Col4": "datetime64"},
dtype={"Col2": "string", "Col3": "Int64", "Col4": "datetime64[ns]"},
parser=parser,
)
df_iter = read_xml_iterparse(
xml_dates,
parser=parser,
names=["Col1", "Col2", "Col3", "Col4"],
dtype={"Col2": "string", "Col3": "Int64", "Col4": "datetime64"},
dtype={"Col2": "string", "Col3": "Int64", "Col4": "datetime64[ns]"},
iterparse={"row": ["shape", "degrees", "sides", "date"]},
)

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/reshape/merge/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,13 +730,13 @@ def test_other_datetime_unit(self, unit):
ser = Series([None, None], index=[101, 102], name="days")

dtype = f"datetime64[{unit}]"
df2 = ser.astype(dtype).to_frame("days")

if unit in ["D", "h", "m"]:
# not supported so we cast to the nearest supported unit, seconds
exp_dtype = "datetime64[s]"
else:
exp_dtype = dtype
df2 = ser.astype(exp_dtype).to_frame("days")
assert df2["days"].dtype == exp_dtype

result = df1.merge(df2, left_on="entity_id", right_index=True)
Expand Down