Skip to content

BUG: fix+test assigning invalid NAT-like to DTA/TDA/PA #27331

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 8 commits into from
Jul 11, 2019
26 changes: 25 additions & 1 deletion pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ def __setitem__(
elif isinstance(value, self._scalar_type):
self._check_compatible_with(value)
value = self._unbox_scalar(value)
elif isna(value) or value == iNaT:
elif is_valid_na(value, self.dtype) or value == iNaT:
value = iNaT
else:
msg = (
Expand Down Expand Up @@ -1679,3 +1679,27 @@ def _ensure_datetimelike_to_i8(other, to_utc=False):
# period array cannot be coerced to int
other = Index(other)
return other.asi8


def is_valid_na(obj, dtype):
"""
isna check that excludes incompatible dtypes

Parameters
----------
obj : object
dtype : np.datetime64, np.timedelta64, DatetimeTZDtype, or PeriodDtype

Returns
-------
bool
"""
if not isna(obj):
return False
if dtype.kind == "M":
return not isinstance(obj, np.timedelta64)
if dtype.kind == "m":
return not isinstance(obj, np.datetime64)

# must be PeriodDType
return not isinstance(obj, (np.datetime64, np.timedelta64))
36 changes: 36 additions & 0 deletions pandas/tests/arrays/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -651,3 +651,39 @@ def test_array_interface(self, period_index):
result = np.asarray(arr, dtype="S20")
expected = np.asarray(arr).astype("S20")
tm.assert_numpy_array_equal(result, expected)

Copy link
Contributor

Choose a reason for hiding this comment

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

are there other setitem tests? put near those


@pytest.mark.parametrize(
"array",
[
pd.TimedeltaIndex(["1 Day", "3 Hours", "NaT"])._data,
pd.date_range("2000-01-01", periods=3, freq="D")._data,
pd.period_range("2000-01-01", periods=3, freq="D")._data,
],
ids=lambda x: type(x).__name__,
)
def test_nat_assignment_array(array):
Copy link
Contributor

Choose a reason for hiding this comment

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

assignment -> setitem

Copy link
Member Author

Choose a reason for hiding this comment

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

changed

expected = type(array)._from_sequence([pd.NaT, array[1], array[2]])

all_nats = [pd.NaT, np.timedelta64("NaT", "ns"), np.datetime64("NaT", "ns")]
casting_nats = {
TimedeltaArray: [pd.NaT, np.timedelta64("NaT", "ns")],
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 too complicated, just provide the expected alongside the values above

DatetimeArray: [pd.NaT, np.datetime64("NaT", "ns")],
PeriodArray: [pd.NaT],
}[type(array)]
non_casting_nats = {
Copy link
Contributor

Choose a reason for hiding this comment

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

this would be better to parameterize over the non_casting_nats and the casting_nats in a separate test. too much going on here.

Copy link
Member Author

Choose a reason for hiding this comment

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

changed

TimedeltaArray: [np.datetime64("NaT", "ns")],
DatetimeArray: [np.timedelta64("NaT", "ns")],
PeriodArray: [np.timedelta64("NaT", "ns"), np.datetime64("NaT", "ns")],
}[type(array)]

for nat in casting_nats:
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 make another level of parameterization to make these truly separate tests?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think so; the casting_nats parameter depends on the array parameter.

I've been looking at pytest extensions that might make this kind of thing possible

arr = array.copy()
arr[0] = nat

tm.assert_equal(arr, expected)

for nat in non_casting_nats:
arr = array.copy()
with pytest.raises(TypeError):
arr[0] = nat