-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Fix passing empty label to df drop #21515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
35742fc
Fix passing empty label to df drop
alimcmaster1 832a50b
Pep8
alimcmaster1 bb80ded
Pep8
alimcmaster1 a81c74a
Update per toobaz comments
alimcmaster1 394b384
Update per comment jreback
alimcmaster1 93bbf05
Pep8
alimcmaster1 1b832d0
Update per comment jreback
alimcmaster1 8119f72
Update per comment jreback
alimcmaster1 9d5a42f
Merge branch 'master' into PR_TOOL_MERGE_PR_21515
jreback 01f6a9c
clean tests
jreback 13b36c2
Parameterize test cases
alimcmaster1 d5f67e1
Merge remote-tracking branch 'origin/df-drop-errors-2' into df-drop-e…
alimcmaster1 1ffc0d4
Parameterize test cases
alimcmaster1 454cb7e
Merge branch 'master' into df-drop-errors-2
toobaz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3129,7 +3129,7 @@ def _drop_axis(self, labels, axis, level=None, errors='raise'): | |
""" | ||
axis = self._get_axis_number(axis) | ||
axis_name = self._get_axis_name(axis) | ||
axis, axis_ = self._get_axis(axis), axis | ||
axis = self._get_axis(axis) | ||
|
||
if axis.is_unique: | ||
if level is not None: | ||
|
@@ -3138,24 +3138,25 @@ def _drop_axis(self, labels, axis, level=None, errors='raise'): | |
new_axis = axis.drop(labels, level=level, errors=errors) | ||
else: | ||
new_axis = axis.drop(labels, errors=errors) | ||
dropped = self.reindex(**{axis_name: new_axis}) | ||
try: | ||
dropped.axes[axis_].set_names(axis.names, inplace=True) | ||
except AttributeError: | ||
pass | ||
result = dropped | ||
result = self.reindex(**{axis_name: new_axis}) | ||
|
||
# Case for non-unique axis | ||
else: | ||
labels = _ensure_object(com._index_labels_to_array(labels)) | ||
if level is not None: | ||
if not isinstance(axis, MultiIndex): | ||
raise AssertionError('axis must be a MultiIndex') | ||
indexer = ~axis.get_level_values(level).isin(labels) | ||
|
||
# GH 18561 MultiIndex.drop should raise if label is absent | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. blank line before the comment |
||
if errors == 'raise' and indexer.all(): | ||
raise KeyError('{} not found in axis'.format(labels)) | ||
else: | ||
indexer = ~axis.isin(labels) | ||
|
||
if errors == 'raise' and indexer.all(): | ||
raise KeyError('{} not found in axis'.format(labels)) | ||
# Check if label doesn't exist along axis | ||
labels_missing = (axis.get_indexer_for(labels) == -1).any() | ||
if errors == 'raise' and labels_missing: | ||
raise KeyError('{} not found in axis'.format(labels)) | ||
|
||
slicer = [slice(None)] * self.ndim | ||
slicer[self._get_axis_number(axis_name)] = indexer | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -472,54 +472,86 @@ def test_rename(): | |
assert result.name == expected.name | ||
|
||
|
||
def test_drop(): | ||
# unique | ||
s = Series([1, 2], index=['one', 'two']) | ||
expected = Series([1], index=['one']) | ||
result = s.drop(['two']) | ||
assert_series_equal(result, expected) | ||
result = s.drop('two', axis='rows') | ||
assert_series_equal(result, expected) | ||
|
||
# non-unique | ||
# GH 5248 | ||
s = Series([1, 1, 2], index=['one', 'two', 'one']) | ||
expected = Series([1, 2], index=['one', 'one']) | ||
result = s.drop(['two'], axis=0) | ||
assert_series_equal(result, expected) | ||
result = s.drop('two') | ||
assert_series_equal(result, expected) | ||
|
||
expected = Series([1], index=['two']) | ||
result = s.drop(['one']) | ||
assert_series_equal(result, expected) | ||
result = s.drop('one') | ||
assert_series_equal(result, expected) | ||
@pytest.mark.parametrize( | ||
'data, index, drop_labels,' | ||
' axis, expected_data, expected_index', | ||
[ | ||
# Unique Index | ||
([1, 2], ['one', 'two'], ['two'], | ||
0, [1], ['one']), | ||
([1, 2], ['one', 'two'], ['two'], | ||
'rows', [1], ['one']), | ||
([1, 1, 2], ['one', 'two', 'one'], ['two'], | ||
0, [1, 2], ['one', 'one']), | ||
|
||
# GH 5248 Non-Unique Index | ||
([1, 1, 2], ['one', 'two', 'one'], 'two', | ||
0, [1, 2], ['one', 'one']), | ||
([1, 1, 2], ['one', 'two', 'one'], ['one'], | ||
0, [1], ['two']), | ||
([1, 1, 2], ['one', 'two', 'one'], 'one', | ||
0, [1], ['two'])]) | ||
def test_drop_unique_and_non_unique_index(data, index, axis, drop_labels, | ||
expected_data, expected_index): | ||
|
||
s = Series(data=data, index=index) | ||
result = s.drop(drop_labels, axis=axis) | ||
expected = Series(data=expected_data, index=expected_index) | ||
tm.assert_series_equal(result, expected) | ||
|
||
# single string/tuple-like | ||
s = Series(range(3), index=list('abc')) | ||
pytest.raises(KeyError, s.drop, 'bc') | ||
pytest.raises(KeyError, s.drop, ('a',)) | ||
|
||
@pytest.mark.parametrize( | ||
'data, index, drop_labels,' | ||
' axis, error_type, error_desc', | ||
[ | ||
# single string/tuple-like | ||
(range(3), list('abc'), 'bc', | ||
0, KeyError, 'not found in axis'), | ||
|
||
# bad axis | ||
(range(3), list('abc'), ('a',), | ||
0, KeyError, 'not found in axis'), | ||
(range(3), list('abc'), 'one', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @jreback that't much neater than my attempt! |
||
'columns', ValueError, 'No axis named columns')]) | ||
def test_drop_exception_raised(data, index, drop_labels, | ||
axis, error_type, error_desc): | ||
|
||
with tm.assert_raises_regex(error_type, error_desc): | ||
Series(data, index=index).drop(drop_labels, axis=axis) | ||
|
||
|
||
def test_drop_with_ignore_errors(): | ||
# errors='ignore' | ||
s = Series(range(3), index=list('abc')) | ||
result = s.drop('bc', errors='ignore') | ||
assert_series_equal(result, s) | ||
tm.assert_series_equal(result, s) | ||
result = s.drop(['a', 'd'], errors='ignore') | ||
expected = s.iloc[1:] | ||
assert_series_equal(result, expected) | ||
|
||
# bad axis | ||
pytest.raises(ValueError, s.drop, 'one', axis='columns') | ||
tm.assert_series_equal(result, expected) | ||
|
||
# GH 8522 | ||
s = Series([2, 3], index=[True, False]) | ||
assert s.index.is_object() | ||
result = s.drop(True) | ||
expected = Series([3], index=[False]) | ||
assert_series_equal(result, expected) | ||
tm.assert_series_equal(result, expected) | ||
|
||
|
||
# GH 16877 | ||
s = Series([2, 3], index=[0, 1]) | ||
with tm.assert_raises_regex(KeyError, 'not contained in axis'): | ||
s.drop([False, True]) | ||
@pytest.mark.parametrize('index', [[1, 2, 3], [1, 1, 3]]) | ||
@pytest.mark.parametrize('drop_labels', [[], [1], [3]]) | ||
def test_drop_empty_list(index, drop_labels): | ||
# GH 21494 | ||
expected_index = [i for i in index if i not in drop_labels] | ||
series = pd.Series(index=index).drop(drop_labels) | ||
tm.assert_series_equal(series, pd.Series(index=expected_index)) | ||
|
||
|
||
@pytest.mark.parametrize('data, index, drop_labels', [ | ||
(None, [1, 2, 3], [1, 4]), | ||
(None, [1, 2, 2], [1, 4]), | ||
([2, 3], [0, 1], [False, True]) | ||
]) | ||
def test_drop_non_empty_list(data, index, drop_labels): | ||
# GH 21494 and GH 16877 | ||
with tm.assert_raises_regex(KeyError, 'not found in axis'): | ||
pd.Series(data=data, index=index).drop(drop_labels) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment to indicate this is the non-unique case