Skip to content

Commit 1b832d0

Browse files
committed
Update per comment jreback
1 parent 93bbf05 commit 1b832d0

File tree

5 files changed

+96
-97
lines changed

5 files changed

+96
-97
lines changed

pandas/core/generic.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3140,12 +3140,14 @@ def _drop_axis(self, labels, axis, level=None, errors='raise'):
31403140
new_axis = axis.drop(labels, errors=errors)
31413141
result = self.reindex(**{axis_name: new_axis})
31423142

3143+
# Case for non-unique axis
31433144
else:
31443145
labels = _ensure_object(com._index_labels_to_array(labels))
31453146
if level is not None:
31463147
if not isinstance(axis, MultiIndex):
31473148
raise AssertionError('axis must be a MultiIndex')
31483149
indexer = ~axis.get_level_values(level).isin(labels)
3150+
31493151
# GH 18561 MultiIndex.drop should raise if label is absent
31503152
if errors == 'raise' and indexer.all():
31513153
raise KeyError('{} not found in axis'.format(labels))

pandas/tests/frame/test_axis_select_reindex.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,3 +1151,24 @@ def test_raise_on_drop_duplicate_index(self, actual):
11511151
expected_no_err = actual.T.drop('c', axis=1, level=level,
11521152
errors='ignore')
11531153
assert_frame_equal(expected_no_err.T, actual)
1154+
1155+
@pytest.mark.parametrize('index, drop_labels', [
1156+
([1, 2, 3], []),
1157+
([1, 1, 2], []),
1158+
([1, 2, 3], [2]),
1159+
([1, 1, 3], [1]),
1160+
])
1161+
def test_drop_empty_list(self, index, drop_labels):
1162+
# GH 21494
1163+
expected_index = [i for i in index if i not in drop_labels]
1164+
frame = pd.DataFrame(index=index).drop(drop_labels)
1165+
tm.assert_frame_equal(frame, pd.DataFrame(index=expected_index))
1166+
1167+
@pytest.mark.parametrize('index, drop_labels', [
1168+
([1, 2, 3], [1, 4]),
1169+
([1, 2, 2], [1, 4]),
1170+
])
1171+
def test_drop_non_empty_list(self, index, drop_labels):
1172+
# GH 21494
1173+
with tm.assert_raises_regex(KeyError, 'not found in axis'):
1174+
pd.DataFrame(index=index).drop(drop_labels)

pandas/tests/frame/test_indexing.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3515,24 +3515,3 @@ def test_functions_no_warnings(self):
35153515
with tm.assert_produces_warning(False):
35163516
df['group'] = pd.cut(df.value, range(0, 105, 10), right=False,
35173517
labels=labels)
3518-
3519-
@pytest.mark.parametrize('index, drop_labels', [
3520-
([1, 2, 3], []),
3521-
([1, 1, 2], []),
3522-
([1, 2, 3], [2]),
3523-
([1, 1, 3], [1]),
3524-
])
3525-
def test_drop_empty_list(self, index, drop_labels):
3526-
# GH 21494
3527-
expected_index = [i for i in index if i not in drop_labels]
3528-
frame = pd.DataFrame(index=index).drop(drop_labels)
3529-
tm.assert_frame_equal(frame, pd.DataFrame(index=expected_index))
3530-
3531-
@pytest.mark.parametrize('index, drop_labels', [
3532-
([1, 2, 3], [1, 4]),
3533-
([1, 2, 2], [1, 4]),
3534-
])
3535-
def test_drop_non_empty_list(self, index, drop_labels):
3536-
# GH 21494
3537-
with tm.assert_raises_regex(KeyError, 'not found in axis'):
3538-
pd.DataFrame(index=index).drop(drop_labels)

pandas/tests/indexes/test_base.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,6 +1565,79 @@ def test_drop_tuple(self, values, to_drop):
15651565
for drop_me in to_drop[1], [to_drop[1]]:
15661566
pytest.raises(KeyError, removed.drop, drop_me)
15671567

1568+
def test_drop_unique_and_non_unique_index(self):
1569+
# unique
1570+
s = Series([1, 2], index=['one', 'two'])
1571+
expected = Series([1], index=['one'])
1572+
result = s.drop(['two'])
1573+
tm.assert_series_equal(result, expected)
1574+
result = s.drop('two', axis='rows')
1575+
tm.assert_series_equal(result, expected)
1576+
1577+
# non-unique
1578+
# GH 5248
1579+
s = Series([1, 1, 2], index=['one', 'two', 'one'])
1580+
expected = Series([1, 2], index=['one', 'one'])
1581+
result = s.drop(['two'], axis=0)
1582+
tm.assert_series_equal(result, expected)
1583+
result = s.drop('two')
1584+
tm.assert_series_equal(result, expected)
1585+
1586+
expected = Series([1], index=['two'])
1587+
result = s.drop(['one'])
1588+
tm.assert_series_equal(result, expected)
1589+
result = s.drop('one')
1590+
tm.assert_series_equal(result, expected)
1591+
1592+
# single string/tuple-like
1593+
s = Series(range(3), index=list('abc'))
1594+
pytest.raises(KeyError, s.drop, 'bc')
1595+
pytest.raises(KeyError, s.drop, ('a',))
1596+
1597+
# errors='ignore'
1598+
s = Series(range(3), index=list('abc'))
1599+
result = s.drop('bc', errors='ignore')
1600+
tm.assert_series_equal(result, s)
1601+
result = s.drop(['a', 'd'], errors='ignore')
1602+
expected = s.iloc[1:]
1603+
tm.assert_series_equal(result, expected)
1604+
1605+
# bad axis
1606+
pytest.raises(ValueError, s.drop, 'one', axis='columns')
1607+
1608+
# GH 8522
1609+
s = Series([2, 3], index=[True, False])
1610+
assert s.index.is_object()
1611+
result = s.drop(True)
1612+
expected = Series([3], index=[False])
1613+
tm.assert_series_equal(result, expected)
1614+
1615+
# GH 16877
1616+
s = Series([2, 3], index=[0, 1])
1617+
with tm.assert_raises_regex(KeyError, 'not found in axis'):
1618+
s.drop([False, True])
1619+
1620+
@pytest.mark.parametrize('index, drop_labels', [
1621+
([1, 2, 3], []),
1622+
([1, 1, 2], []),
1623+
([1, 2, 3], [2]),
1624+
([1, 1, 3], [1]),
1625+
])
1626+
def test_drop_empty_list(self, index, drop_labels):
1627+
# GH 21494
1628+
expected_index = [i for i in index if i not in drop_labels]
1629+
series = pd.Series(index=index).drop(drop_labels)
1630+
tm.assert_series_equal(series, pd.Series(index=expected_index))
1631+
1632+
@pytest.mark.parametrize('index, drop_labels', [
1633+
([1, 2, 3], [1, 4]),
1634+
([1, 2, 2], [1, 4]),
1635+
])
1636+
def test_drop_non_empty_list(self, index, drop_labels):
1637+
# GH 21494
1638+
with tm.assert_raises_regex(KeyError, 'not found in axis'):
1639+
pd.Series(index=index).drop(drop_labels)
1640+
15681641
@pytest.mark.parametrize("method,expected", [
15691642
('intersection', np.array([(1, 'A'), (2, 'A'), (1, 'B'), (2, 'B')],
15701643
dtype=[('num', int), ('let', 'a1')])),

pandas/tests/series/indexing/test_alter_index.py

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -461,79 +461,3 @@ def test_rename():
461461
assert_series_equal(result, expected)
462462

463463
assert result.name == expected.name
464-
465-
466-
def test_drop():
467-
# unique
468-
s = Series([1, 2], index=['one', 'two'])
469-
expected = Series([1], index=['one'])
470-
result = s.drop(['two'])
471-
assert_series_equal(result, expected)
472-
result = s.drop('two', axis='rows')
473-
assert_series_equal(result, expected)
474-
475-
# non-unique
476-
# GH 5248
477-
s = Series([1, 1, 2], index=['one', 'two', 'one'])
478-
expected = Series([1, 2], index=['one', 'one'])
479-
result = s.drop(['two'], axis=0)
480-
assert_series_equal(result, expected)
481-
result = s.drop('two')
482-
assert_series_equal(result, expected)
483-
484-
expected = Series([1], index=['two'])
485-
result = s.drop(['one'])
486-
assert_series_equal(result, expected)
487-
result = s.drop('one')
488-
assert_series_equal(result, expected)
489-
490-
# single string/tuple-like
491-
s = Series(range(3), index=list('abc'))
492-
pytest.raises(KeyError, s.drop, 'bc')
493-
pytest.raises(KeyError, s.drop, ('a',))
494-
495-
# errors='ignore'
496-
s = Series(range(3), index=list('abc'))
497-
result = s.drop('bc', errors='ignore')
498-
assert_series_equal(result, s)
499-
result = s.drop(['a', 'd'], errors='ignore')
500-
expected = s.iloc[1:]
501-
assert_series_equal(result, expected)
502-
503-
# bad axis
504-
pytest.raises(ValueError, s.drop, 'one', axis='columns')
505-
506-
# GH 8522
507-
s = Series([2, 3], index=[True, False])
508-
assert s.index.is_object()
509-
result = s.drop(True)
510-
expected = Series([3], index=[False])
511-
assert_series_equal(result, expected)
512-
513-
# GH 16877
514-
s = Series([2, 3], index=[0, 1])
515-
with tm.assert_raises_regex(KeyError, 'not found in axis'):
516-
s.drop([False, True])
517-
518-
519-
@pytest.mark.parametrize('index, drop_labels', [
520-
([1, 2, 3], []),
521-
([1, 1, 2], []),
522-
([1, 2, 3], [2]),
523-
([1, 1, 3], [1]),
524-
])
525-
def test_drop_empty_list(index, drop_labels):
526-
# GH 21494
527-
expected_index = [i for i in index if i not in drop_labels]
528-
series = pd.Series(index=index).drop(drop_labels)
529-
tm.assert_series_equal(series, pd.Series(index=expected_index))
530-
531-
532-
@pytest.mark.parametrize('index, drop_labels', [
533-
([1, 2, 3], [1, 4]),
534-
([1, 2, 2], [1, 4]),
535-
])
536-
def test_drop_non_empty_list(index, drop_labels):
537-
# GH 21494
538-
with tm.assert_raises_regex(KeyError, 'not found in axis'):
539-
pd.Series(index=index).drop(drop_labels)

0 commit comments

Comments
 (0)