Skip to content

Commit 962ca2c

Browse files
benjaminmgrossjreback
authored andcommitted
TST: tests for Index.insert to validate neg/pos indexers (GH7256)
1 parent fa0f78f commit 962ca2c

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

pandas/core/index.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1774,7 +1774,8 @@ def delete(self, loc):
17741774

17751775
def insert(self, loc, item):
17761776
"""
1777-
Make new Index inserting new item at location
1777+
Make new Index inserting new item at location. Follows
1778+
Python list.append semantics for negative values
17781779
17791780
Parameters
17801781
----------

pandas/tests/test_index.py

+23
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,29 @@ def test_equals(self):
242242
# Must also be an Index
243243
self.assertFalse(Index(['a', 'b', 'c']).equals(['a', 'b', 'c']))
244244

245+
def test_insert(self):
246+
247+
# GH 7256
248+
# validate neg/pos inserts
249+
result = Index(['b', 'c', 'd'])
250+
251+
#test 0th element
252+
self.assertTrue(Index(['a', 'b', 'c', 'd']).equals(
253+
result.insert(0, 'a')))
254+
255+
#test Nth element that follows Python list behavior
256+
self.assertTrue(Index(['b', 'c', 'e', 'd']).equals(
257+
result.insert(-1, 'e')))
258+
259+
#test loc +/- neq (0, -1)
260+
self.assertTrue(result.insert(1, 'z').equals(
261+
result.insert(-2, 'z')))
262+
263+
#test empty
264+
null_index = Index([])
265+
self.assertTrue(Index(['a']).equals(
266+
null_index.insert(0, 'a')))
267+
245268
def test_identical(self):
246269

247270
# index

0 commit comments

Comments
 (0)