Skip to content

BUG: TDI.insert with empty TDI raising IndexError #30757

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 10 commits into from
Jan 9, 2020
8 changes: 4 additions & 4 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Base and utility classes for tseries type pandas objects.
"""
import operator
from typing import List, Optional, Set
from typing import List, Optional, Set, Union

import numpy as np

Expand Down Expand Up @@ -30,8 +30,8 @@
from pandas.core.accessor import PandasDelegate
from pandas.core.arrays import (
DatetimeArray,
ExtensionArray,
ExtensionOpsMixin,
PeriodArray,
TimedeltaArray,
)
from pandas.core.arrays.datetimelike import (
Expand Down Expand Up @@ -96,7 +96,7 @@ class DatetimeIndexOpsMixin(ExtensionIndex, ExtensionOpsMixin):
Common ops mixin to support a unified interface datetimelike Index.
"""

_data: ExtensionArray
_data: Union[DatetimeArray, TimedeltaArray, PeriodArray]
Copy link
Contributor

Choose a reason for hiding this comment

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

should define this with a short label, maybe DatetimeArrayType

freq: Optional[DateOffset]
freqstr: Optional[str]
_resolution: int
Expand Down Expand Up @@ -950,7 +950,7 @@ class DatetimelikeDelegateMixin(PandasDelegate):
_raw_methods: Set[str] = set()
# raw_properties : dispatch properties that shouldn't be boxed in an Index
_raw_properties: Set[str] = set()
_data: ExtensionArray
_data: Union[DatetimeArray, TimedeltaArray, PeriodArray]

def _delegate_property_get(self, name, *args, **kwargs):
result = getattr(self._data, name)
Expand Down
11 changes: 5 additions & 6 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ def inferred_type(self) -> str:
# sure we can't have ambiguous indexing
return "datetime64"

def insert(self, loc, item):
def insert(self, loc: int, item):
"""
Make new Index inserting new item at location

Expand All @@ -933,10 +933,9 @@ def insert(self, loc, item):

freq = None

if isinstance(item, (datetime, np.datetime64)):
self._assert_can_do_op(item)
if not self._has_same_tz(item) and not isna(item):
raise ValueError("Passed item and index have different timezone")
if isinstance(item, self._data._recognized_scalars) or item is NaT:
item = self._data._scalar_type(item)
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:
Expand All @@ -946,7 +945,7 @@ def insert(self, loc, item):
freq = self.freq
elif (loc == len(self)) and item - self.freq == self[-1]:
freq = self.freq
item = _to_M8(item, tz=self.tz)
item = item.asm8
Copy link
Contributor

Choose a reason for hiding this comment

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

can we remove _to_M8?

Copy link
Member Author

Choose a reason for hiding this comment

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

can we remove _to_M8?

IIRC there's still one more usage to get rid of after this. I'm eager to get rid of it, as im pretty sure it is hiding a bunch of corner case bugs.


try:
new_dates = np.concatenate(
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ def _convert_tolerance(self, tolerance, target):
raise ValueError("list-like tolerance size must match target index size")
return self._maybe_convert_timedelta(tolerance)

def insert(self, loc, item):
def insert(self, loc: int, item):
if not isinstance(item, Period) or self.freq != item.freq:
return self.astype(object).insert(loc, item)

Expand Down
14 changes: 9 additions & 5 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ def is_type_compatible(self, typ) -> bool:
def inferred_type(self) -> str:
return "timedelta64"

def insert(self, loc, item):
def insert(self, loc: int, item):
"""
Make new Index inserting new item at location

Expand Down Expand Up @@ -409,15 +409,19 @@ def insert(self, loc, item):
)

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

# check freq can be preserved on edge cases
if self.freq is not None:
if (loc == 0 or loc == -len(self)) and item + self.freq == self[0]:
if self.size and self.freq is not None:
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
item = Timedelta(item).asm8.view(_TD_DTYPE)
item = item.asm8

try:
new_tds = np.concatenate(
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/datetimes/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,9 @@ def test_insert(self):

# see gh-7299
idx = date_range("1/1/2000", periods=3, freq="D", tz="Asia/Tokyo", name="idx")
with pytest.raises(ValueError):
with pytest.raises(TypeError):
idx.insert(3, pd.Timestamp("2000-01-04"))
with pytest.raises(ValueError):
with pytest.raises(TypeError):
idx.insert(3, datetime(2000, 1, 4))
with pytest.raises(ValueError):
idx.insert(3, pd.Timestamp("2000-01-04", tz="US/Eastern"))
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/indexes/timedeltas/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,15 @@ def test_take_fill_value(self):


class TestTimedeltaIndex:
def test_insert_empty(self):
# Corner case inserting with length zero doesnt raise IndexError
idx = timedelta_range("1 Day", periods=3)
td = idx[0]

idx[:0].insert(0, td)
idx[:0].insert(1, td)
idx[:0].insert(-1, td)

Copy link
Contributor

Choose a reason for hiding this comment

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

maybe worth comparing the results

def test_insert(self):

idx = TimedeltaIndex(["4day", "1day", "2day"], name="idx")
Expand Down
14 changes: 10 additions & 4 deletions pandas/tests/indexing/test_coercion.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,13 +432,19 @@ def test_insert_index_datetimes(self, fill_val, exp_dtype):
)
self._assert_insert_conversion(obj, fill_val, exp, exp_dtype)

msg = "Passed item and index have different timezone"
if fill_val.tz:
with pytest.raises(ValueError, match=msg):
msg = "Cannot compare tz-naive and tz-aware datetime-like objects"
with pytest.raises(TypeError, match=msg):
obj.insert(1, pd.Timestamp("2012-01-01"))

with pytest.raises(ValueError, match=msg):
obj.insert(1, pd.Timestamp("2012-01-01", tz="Asia/Tokyo"))
msg = "Timezones don't match"
with pytest.raises(ValueError, match=msg):
obj.insert(1, pd.Timestamp("2012-01-01", tz="Asia/Tokyo"))

else:
msg = "Cannot compare tz-naive and tz-aware datetime-like objects"
with pytest.raises(TypeError, match=msg):
obj.insert(1, pd.Timestamp("2012-01-01", tz="Asia/Tokyo"))

msg = "cannot insert DatetimeIndex with incompatible label"
with pytest.raises(TypeError, match=msg):
Expand Down