Skip to content

Commit 6ec9624

Browse files
committed
Drop & insert on subtypes of index return their subtypes
1 parent bd73cd0 commit 6ec9624

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

doc/source/whatsnew/v0.17.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -401,4 +401,5 @@ Bug Fixes
401401
- Bug in ``io.common.get_filepath_or_buffer`` which caused reading of valid S3 files to fail if the bucket also contained keys for which the user does not have read permission (:issue:`10604`)
402402
- Bug in vectorised setting of timestamp columns with python ``datetime.date`` and numpy ``datetime64`` (:issue:`10408`, :issue:`10412`)
403403
- Bug in ``pd.DataFrame`` when constructing an empty DataFrame with a string dtype (:issue:`9428`)
404+
- Bug in `Index` subtypes (such as `PeriodIndex`) not returning their own type for `.drop` and `.insert` methods
404405

pandas/core/index.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -1548,8 +1548,16 @@ def sym_diff(self, other, result_name=None):
15481548
other, result_name_update = self._convert_can_do_setop(other)
15491549
if result_name is None:
15501550
result_name = result_name_update
1551-
the_diff = sorted(set((self.difference(other)).union(other.difference(self))))
1552-
return Index(the_diff, name=result_name)
1551+
the_diff_sorted = sorted(set((self.difference(other)).union(other.difference(self))))
1552+
if isinstance(self, MultiIndex):
1553+
# multiindexes don't currently work well with _shallow_copy & can't be supplied a name
1554+
return Index(the_diff_sorted, **self._get_attributes_dict())
1555+
else:
1556+
# convert list to Index and pull values out - required for subtypes such as PeriodIndex
1557+
diff_array = Index(the_diff_sorted).values
1558+
return self._shallow_copy(diff_array, name=result_name)
1559+
1560+
15531561

15541562
def get_loc(self, key, method=None):
15551563
"""
@@ -2527,7 +2535,7 @@ def delete(self, loc):
25272535
-------
25282536
new_index : Index
25292537
"""
2530-
return Index(np.delete(self._data, loc), name=self.name)
2538+
return self._shallow_copy(np.delete(self._data, loc))
25312539

25322540
def insert(self, loc, item):
25332541
"""
@@ -2543,11 +2551,12 @@ def insert(self, loc, item):
25432551
-------
25442552
new_index : Index
25452553
"""
2546-
_self = np.asarray(self)
2547-
item_idx = Index([item], dtype=self.dtype).values
2548-
idx = np.concatenate(
2549-
(_self[:loc], item_idx, _self[loc:]))
2550-
return Index(idx, name=self.name)
2554+
indexes=[self[:loc],
2555+
Index([item]),
2556+
self[loc:]]
2557+
2558+
return indexes[0].append(indexes[1]).append(indexes[2])
2559+
25512560

25522561
def drop(self, labels, errors='raise'):
25532562
"""

pandas/tests/test_index.py

+31
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,37 @@ def test_symmetric_diff(self):
396396
with tm.assertRaisesRegexp(TypeError, msg):
397397
result = first.sym_diff([1, 2, 3])
398398

399+
def test_insert_base(self):
400+
401+
for name, idx in compat.iteritems(self.indices):
402+
result = idx[1:4]
403+
404+
if len(idx)>0:
405+
#test 0th element
406+
self.assertTrue(idx[0:4].equals(
407+
result.insert(0, idx[0])))
408+
409+
def test_delete_base(self):
410+
411+
for name, idx in compat.iteritems(self.indices):
412+
413+
if len(idx)>0:
414+
415+
expected = idx[1:]
416+
result = idx.delete(0)
417+
self.assertTrue(result.equals(expected))
418+
self.assertEqual(result.name, expected.name)
419+
420+
expected = idx[:-1]
421+
result = idx.delete(-1)
422+
self.assertTrue(result.equals(expected))
423+
self.assertEqual(result.name, expected.name)
424+
425+
with tm.assertRaises((IndexError, ValueError)):
426+
# either depending on numpy version
427+
result = idx.delete(len(idx))
428+
429+
399430
def test_equals_op(self):
400431
# GH9947, GH10637
401432
index_a = self.create_index()

0 commit comments

Comments
 (0)