Skip to content

BUG: DTI/TDI.insert doing invalid casting #33703

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 5 commits into from
Apr 25, 2020
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
5 changes: 4 additions & 1 deletion pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -857,10 +857,13 @@ def _validate_setitem_value(self, value):
def _validate_insert_value(self, value):
if isinstance(value, self._recognized_scalars):
value = self._scalar_type(value)
self._check_compatible_with(value, setitem=True)
# TODO: if we dont have compat, should we raise or astype(object)?
# PeriodIndex does astype(object)
elif is_valid_nat_for_dtype(value, self.dtype):
# GH#18295
value = NaT
elif lib.is_scalar(value) and isna(value):
else:
raise TypeError(
f"cannot insert {type(self).__name__} with incompatible label"
)
Expand Down
33 changes: 13 additions & 20 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -961,37 +961,30 @@ def insert(self, loc, item):
-------
new_index : Index
"""
if isinstance(item, str):
# TODO: Why are strings special?
# TODO: Should we attempt _scalar_from_string?
return self.astype(object).insert(loc, item)

item = self._data._validate_insert_value(item)

freq = None
if isinstance(item, self._data._scalar_type) or item is NaT:
self._data._check_compatible_with(item, setitem=True)

# check freq can be preserved on edge cases
if self.size and self.freq is not None:
# check freq can be preserved on edge cases
if self.freq is not None:
if self.size:
if item is NaT:
pass
elif (loc == 0 or loc == -len(self)) and item + self.freq == self[0]:
freq = self.freq
elif (loc == len(self)) and item - self.freq == self[-1]:
freq = self.freq
elif self.freq is not None:
else:
# Adding a single item to an empty index may preserve freq
if self.freq.is_on_offset(item):
freq = self.freq
item = item.asm8

try:
new_i8s = np.concatenate(
(self[:loc].asi8, [item.view(np.int64)], self[loc:].asi8)
)
arr = type(self._data)._simple_new(new_i8s, dtype=self.dtype, freq=freq)
return type(self)._simple_new(arr, name=self.name)
except (AttributeError, TypeError) as err:
item = self._data._unbox_scalar(item)

# fall back to object index
if isinstance(item, str):
return self.astype(object).insert(loc, item)
raise TypeError(
f"cannot insert {type(self).__name__} with incompatible label"
) from err
new_i8s = np.concatenate([self[:loc].asi8, [item], self[loc:].asi8])
arr = type(self._data)._simple_new(new_i8s, dtype=self.dtype, freq=freq)
return type(self)._simple_new(arr, name=self.name)
23 changes: 23 additions & 0 deletions pandas/tests/indexes/datetimes/test_insert.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,26 @@ def test_insert(self):
assert result.name == expected.name
assert result.tz == expected.tz
assert result.freq is None

@pytest.mark.parametrize(
"item", [0, np.int64(0), np.float64(0), np.array(0), np.timedelta64(456)]
)
def test_insert_mismatched_types_raises(self, tz_aware_fixture, item):
# GH#33703 dont cast these to dt64
tz = tz_aware_fixture
dti = date_range("2019-11-04", periods=9, freq="-1D", name=9, tz=tz)

msg = "incompatible label"
with pytest.raises(TypeError, match=msg):
dti.insert(1, item)

def test_insert_object_casting(self, tz_aware_fixture):
# GH#33703
tz = tz_aware_fixture
dti = date_range("2019-11-04", periods=3, freq="-1D", name=9, tz=tz)

# ATM we treat this as a string, but we could plausibly wrap it in Timestamp
value = "2019-11-05"
result = dti.insert(0, value)
expected = Index(["2019-11-05"] + list(dti), dtype=object, name=9)
tm.assert_index_equal(result, expected)
11 changes: 11 additions & 0 deletions pandas/tests/indexes/timedeltas/test_insert.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ def test_insert_invalid_na(self):
with pytest.raises(TypeError, match="incompatible label"):
idx.insert(0, np.datetime64("NaT"))

@pytest.mark.parametrize(
"item", [0, np.int64(0), np.float64(0), np.array(0), np.datetime64(456, "us")]
)
def test_insert_mismatched_types_raises(self, item):
# GH#33703 dont cast these to td64
tdi = TimedeltaIndex(["4day", "1day", "2day"], name="idx")

msg = "incompatible label"
with pytest.raises(TypeError, match=msg):
tdi.insert(1, item)

def test_insert_dont_cast_strings(self):
# To match DatetimeIndex and PeriodIndex behavior, dont try to
# parse strings to Timedelta
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/indexing/test_coercion.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ def test_insert_index_datetimes(self, fill_val, exp_dtype):
with pytest.raises(TypeError, match=msg):
obj.insert(1, pd.Timestamp("2012-01-01", tz="Asia/Tokyo"))

msg = "cannot insert DatetimeIndex with incompatible label"
msg = "cannot insert DatetimeArray with incompatible label"
with pytest.raises(TypeError, match=msg):
obj.insert(1, 1)

Expand All @@ -464,12 +464,12 @@ def test_insert_index_timedelta64(self):
)

# ToDo: must coerce to object
msg = "cannot insert TimedeltaIndex with incompatible label"
msg = "cannot insert TimedeltaArray with incompatible label"
with pytest.raises(TypeError, match=msg):
obj.insert(1, pd.Timestamp("2012-01-01"))

# ToDo: must coerce to object
msg = "cannot insert TimedeltaIndex with incompatible label"
msg = "cannot insert TimedeltaArray with incompatible label"
with pytest.raises(TypeError, match=msg):
obj.insert(1, 1)

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexing/test_partial.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def test_partial_set_invalid(self):
df = orig.copy()

# don't allow not string inserts
msg = "cannot insert DatetimeIndex with incompatible label"
msg = "cannot insert DatetimeArray with incompatible label"

with pytest.raises(TypeError, match=msg):
df.loc[100.0, :] = df.iloc[0]
Expand Down