Skip to content

Commit 06a4d91

Browse files
max-sixtyjreback
authored andcommitted
Drop & insert on subtypes of index return their subtypes, #10620
1 parent bd73cd0 commit 06a4d91

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

doc/source/whatsnew/v0.17.0.txt

+3
Original file line numberDiff line numberDiff line change
@@ -400,5 +400,8 @@ Bug Fixes
400400
- Bug in `read_msgpack` where DataFrame to decode has duplicate column names (:issue:`9618`)
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`)
403+
403404
- Bug in ``pd.DataFrame`` when constructing an empty DataFrame with a string dtype (:issue:`9428`)
405+
- Bug in `Index` subtypes (such as `PeriodIndex`) not returning their own type for `.drop` and `.insert` methods
404406

407+
- Bug in `Index` subtypes (such as `PeriodIndex`) not returning their own type for `.drop` and `.insert` methods

pandas/core/index.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -1548,8 +1548,13 @@ 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+
15511552
the_diff = sorted(set((self.difference(other)).union(other.difference(self))))
1552-
return Index(the_diff, name=result_name)
1553+
attribs = self._get_attributes_dict()
1554+
attribs['name'] = result_name
1555+
if 'freq' in attribs:
1556+
attribs['freq'] = None
1557+
return self._shallow_copy(the_diff, infer=True, **attribs)
15531558

15541559
def get_loc(self, key, method=None):
15551560
"""
@@ -2527,7 +2532,7 @@ def delete(self, loc):
25272532
-------
25282533
new_index : Index
25292534
"""
2530-
return Index(np.delete(self._data, loc), name=self.name)
2535+
return self._shallow_copy(np.delete(self._data, loc))
25312536

25322537
def insert(self, loc, item):
25332538
"""
@@ -2543,11 +2548,12 @@ def insert(self, loc, item):
25432548
-------
25442549
new_index : Index
25452550
"""
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)
2551+
indexes=[self[:loc],
2552+
Index([item]),
2553+
self[loc:]]
2554+
2555+
return indexes[0].append(indexes[1]).append(indexes[2])
2556+
25512557

25522558
def drop(self, labels, errors='raise'):
25532559
"""

pandas/tests/test_index.py

+30
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,36 @@ 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+
399429
def test_equals_op(self):
400430
# GH9947, GH10637
401431
index_a = self.create_index()

0 commit comments

Comments
 (0)