-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Raise when casting NaT to int #28492
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
Changes from 3 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 |
---|---|---|
|
@@ -8,6 +8,8 @@ | |
from pandas._libs.tslibs import NaT, OutOfBoundsDatetime, Period, iNaT | ||
from pandas.util._validators import validate_bool_kwarg | ||
|
||
import pandas as pd | ||
|
||
from .common import ( | ||
_INT64_DTYPE, | ||
_NS_DTYPE, | ||
|
@@ -696,6 +698,8 @@ def astype_nansafe(arr, dtype, copy=True, skipna=False): | |
if is_object_dtype(dtype): | ||
return tslib.ints_to_pydatetime(arr.view(np.int64)) | ||
elif dtype == np.int64: | ||
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 likely needs to be is_integer_dtype(dtype) 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. I think this might actually need to be |
||
if pd.isnull(arr).any(): | ||
raise ValueError("Cannot convert NaT values to integer") | ||
return arr.view(dtype) | ||
|
||
# allow frequency conversions | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -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, | ||||
|
@@ -731,3 +732,11 @@ def test__is_dtype_type_sparse(): | |||
result = np.dtype("int32") | ||||
assert com._is_dtype_type(ser, lambda tipo: tipo == result) | ||||
assert com._is_dtype_type(ser.dtype, lambda tipo: tipo == result) | ||||
|
||||
|
||||
def test__nansafe_nat_to_int(): | ||||
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. rename to test_astype_nansafe; can you parameterize this on all ints at the very least. ideally we would have more testing for this (but can be followup) 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. For the other (non-int64) dtypes we end up hitting the pandas/pandas/core/dtypes/cast.py Line 707 in b81f433
Would you recommend breaking these out into a separate test? |
||||
arr = np.array([np.datetime64("NaT")]) | ||||
dsaxton marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
|
||||
msg = "Cannot convert NaT values to integer" | ||||
with pytest.raises(ValueError, match=msg): | ||||
astype_nansafe(arr, dtype=np.int64) |
Uh oh!
There was an error while loading. Please reload this page.