Skip to content

TST: Add astype_nansafe datetime tests #28533

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
wants to merge 3 commits into from
Closed
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
22 changes: 22 additions & 0 deletions pandas/tests/dtypes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pandas.util._test_decorators as td

from pandas.core.dtypes.cast import astype_nansafe
import pandas.core.dtypes.common as com
from pandas.core.dtypes.dtypes import (
CategoricalDtype,
Expand Down Expand Up @@ -706,3 +707,24 @@ def test__get_dtype_fails(input_param, expected_error_message):
)
def test__is_dtype_type(input_param, result):
assert com._is_dtype_type(input_param, lambda tipo: tipo == result)


@pytest.mark.parametrize("from_type", [np.datetime64, np.timedelta64])
@pytest.mark.parametrize(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this get used elsewhere? If so might be good to make a shared fixture for numeric_dtype or something to the effect

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This particular setup might be a little specific in that all the to_types have lesser precision than the from_types, but did you have something in mind?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is ok

"to_type",
[
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a test for the object path (for dtype=) when its a datetime/timedelta

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would the expected output be for that case? I would think it'd agree with astype from numpy when we don't have any NA values but the result is a bit odd:

[ins] In [3]: arr = np.array([np.datetime64("2018")])                                          

[ins] In [4]: arr.astype("object")                                                             
Out[4]: array([datetime.date(2018, 1, 1)], dtype=object)

[ins] In [5]: astype_nansafe(arr, dtype="object")                                              
Out[5]: array([datetime.datetime(1970, 1, 1, 0, 0)], dtype=object)

np.uint8,
np.uint16,
np.uint32,
np.int8,
np.int16,
np.int32,
np.float16,
np.float32,
],
)
def test_astype_datetime64_bad_dtype_raises(from_type, to_type):
arr = np.array([from_type("2018")])

with pytest.raises(TypeError, match="cannot astype"):
astype_nansafe(arr, dtype=to_type)