-
-
Notifications
You must be signed in to change notification settings - Fork 18.2k
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
BUG: Raise when casting NaT to int #28492
Changes from all commits
0c035bc
0d64522
3d91a8f
482d629
975de26
3445dc7
2e9ece9
e23b9a0
b14d752
9419744
b81f433
e5dfa71
18734ff
c9ba768
5da8246
ac501f6
e229844
931e8c5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,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, | ||
|
@@ -13,6 +14,7 @@ | |
IntervalDtype, | ||
PeriodDtype, | ||
) | ||
from pandas.core.dtypes.missing import isna | ||
|
||
import pandas as pd | ||
from pandas.conftest import ( | ||
|
@@ -721,3 +723,42 @@ 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("val", [np.datetime64("NaT"), np.timedelta64("NaT")]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add an additional test for when dtype == object (which is the other path that hits M8 and m8) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would be a test separate from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep |
||
@pytest.mark.parametrize("typ", [np.int64]) | ||
def test_astype_nansafe(val, typ): | ||
arr = np.array([val]) | ||
|
||
msg = "Cannot convert NaT values to integer" | ||
with pytest.raises(ValueError, match=msg): | ||
astype_nansafe(arr, dtype=typ) | ||
|
||
|
||
@pytest.mark.parametrize("from_type", [np.datetime64, np.timedelta64]) | ||
@pytest.mark.parametrize( | ||
"to_type", | ||
[ | ||
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) | ||
|
||
|
||
@pytest.mark.parametrize("from_type", [np.datetime64, np.timedelta64]) | ||
def test_astype_object_preserves_datetime_na(from_type): | ||
arr = np.array([from_type("NaT")]) | ||
result = astype_nansafe(arr, dtype="object") | ||
|
||
assert isna(result)[0] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this likely needs to be is_integer_dtype(dtype)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this might actually need to be
np.int64
since above we check fordatetime64
(getting some weird test failures after this change). Looking at thenumpy
documentation it seems the output ofview
is "unpredictable" when the precision differs between the data types (the shape of the output can even change).