Skip to content

Commit 7c38c20

Browse files
toobazhexgnu
authored andcommitted
BUG: cast to correct dtype in Index.drop() (pandas-dev#18309)
closes pandas-dev#18304
1 parent f8ee98b commit 7c38c20

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed

doc/source/whatsnew/v0.23.0.txt

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

pandas/core/common.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,19 @@ 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):
402+
"""
403+
Transform label or iterable of labels to array, for use in Index.
404+
405+
Parameters
406+
----------
407+
dtype : dtype
408+
If specified, use as dtype of the resulting array, otherwise infer.
409+
410+
Returns
411+
-------
412+
array
413+
"""
402414
if isinstance(labels, (compat.string_types, tuple)):
403415
labels = [labels]
404416

@@ -408,7 +420,7 @@ def _index_labels_to_array(labels):
408420
except TypeError: # non-iterable
409421
labels = [labels]
410422

411-
labels = _asarray_tuplesafe(labels)
423+
labels = _asarray_tuplesafe(labels, dtype=dtype)
412424

413425
return labels
414426

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

pandas/tests/reshape/test_pivot.py

+12
Original file line numberDiff line numberDiff line change
@@ -1631,3 +1631,15 @@ def test_crosstab_dup_index_names(self):
16311631
index=expected_index,
16321632
columns=expected_index)
16331633
tm.assert_frame_equal(result, expected)
1634+
1635+
@pytest.mark.parametrize("names", [['a', ('b', 'c')],
1636+
[('a', 'b'), 'c']])
1637+
def test_crosstab_tuple_name(self, names):
1638+
s1 = pd.Series(range(3), name=names[0])
1639+
s2 = pd.Series(range(1, 4), name=names[1])
1640+
1641+
mi = pd.MultiIndex.from_arrays([range(3), range(1, 4)], names=names)
1642+
expected = pd.Series(1, index=mi).unstack(1, fill_value=0)
1643+
1644+
result = pd.crosstab(s1, s2)
1645+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)