Skip to content

Commit 69cf672

Browse files
committed
BUG: cast to correct dtype in Index.drop()
closes #18304
1 parent e567949 commit 69cf672

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ Indexing
301301
- Bug in :func:`MultiIndex.remove_unused_levels` which would fill nan values (:issue:`18417`)
302302
- Bug in :func:`MultiIndex.from_tuples`` which would fail to take zipped tuples in python3 (:issue:`18434`)
303303
- Bug in :class:`Index` construction from list of mixed type tuples (:issue:`18505`)
304+
- Bug in :func:`Index.drop` when passing a list of both tuples and non-tuples (:issue:`18304`)
304305
- Bug in :class:`IntervalIndex` where empty and purely NA data was constructed inconsistently depending on the construction method (:issue:`18421`)
305306
- Bug in :func:`IntervalIndex.symmetric_difference` where the symmetric difference with a non-``IntervalIndex`` did not raise (:issue:`18475`)
306307
- Bug in indexing a datetimelike ``Index`` that raised ``ValueError`` instead of ``IndexError`` (:issue:`18386`).

pandas/core/common.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ def _asarray_tuplesafe(values, dtype=None):
398398
return result
399399

400400

401-
def _index_labels_to_array(labels):
401+
def _index_labels_to_array(labels, dtype=None):
402402
if isinstance(labels, (compat.string_types, tuple)):
403403
labels = [labels]
404404

@@ -408,7 +408,7 @@ def _index_labels_to_array(labels):
408408
except TypeError: # non-iterable
409409
labels = [labels]
410410

411-
labels = _asarray_tuplesafe(labels)
411+
labels = _asarray_tuplesafe(labels, dtype=dtype)
412412

413413
return labels
414414

pandas/core/indexes/base.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3761,7 +3761,8 @@ def drop(self, labels, errors='raise'):
37613761
-------
37623762
dropped : Index
37633763
"""
3764-
labels = _index_labels_to_array(labels)
3764+
arr_dtype = 'object' if self.dtype == 'object' else None
3765+
labels = _index_labels_to_array(labels, dtype=arr_dtype)
37653766
indexer = self.get_indexer(labels)
37663767
mask = indexer == -1
37673768
if mask.any():

pandas/tests/indexes/test_base.py

+21
Original file line numberDiff line numberDiff line change
@@ -1429,6 +1429,27 @@ def test_drop(self):
14291429
expected = Index([1, 2])
14301430
tm.assert_index_equal(dropped, expected)
14311431

1432+
@pytest.mark.parametrize("values", [['a', 'b', ('c', 'd')],
1433+
['a', ('c', 'd'), 'b'],
1434+
[('c', 'd'), 'a', 'b']])
1435+
@pytest.mark.parametrize("to_drop", [[('c', 'd'), 'a'], ['a', ('c', 'd')]])
1436+
def test_drop_tuple(self, values, to_drop):
1437+
# GH 18304
1438+
index = pd.Index(values)
1439+
expected = pd.Index(['b'])
1440+
1441+
result = index.drop(to_drop)
1442+
tm.assert_index_equal(result, expected)
1443+
1444+
removed = index.drop(to_drop[0])
1445+
for drop_me in to_drop[1], [to_drop[1]]:
1446+
result = removed.drop(drop_me)
1447+
tm.assert_index_equal(result, expected)
1448+
1449+
removed = index.drop(to_drop[1])
1450+
for drop_me in to_drop[1], [to_drop[1]]:
1451+
pytest.raises(ValueError, removed.drop, drop_me)
1452+
14321453
def test_tuple_union_bug(self):
14331454
import pandas
14341455
import numpy as np

0 commit comments

Comments
 (0)