-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
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
Changes from 6 commits
c613f2e
c4a1b3a
b8895e7
6be32da
40f00b9
29f26f3
3c8f6fd
c203c2c
5a6def3
d2a1459
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 |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -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: | ||
|
@@ -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 | ||
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 we remove _to_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.
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( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
||
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. maybe worth comparing the results |
||
def test_insert(self): | ||
|
||
idx = TimedeltaIndex(["4day", "1day", "2day"], name="idx") | ||
|
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.
should define this with a short label, maybe DatetimeArrayType