Skip to content

BUG: fixes 13822, incorrect KeyError string with non-unique columns w… #13845

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.19.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -855,3 +855,4 @@ Bug Fixes

- Bug in ``.to_excel()`` when DataFrame contains a MultiIndex which contains a label with a NaN value (:issue:`13511`)
- Bug in ``pd.read_csv`` in Python 2.x with non-UTF8 encoded, multi-character separated data (:issue:`3404`)
- Bug in ``Index`` raises ``KeyError`` displaying incorrect column when column is not in the df and columns contains duplicate values (:issue:`13822`)
4 changes: 3 additions & 1 deletion pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,9 @@ def _convert_to_indexer(self, obj, axis=0, is_setter=False):
else:
(indexer,
missing) = labels.get_indexer_non_unique(objarr)
check = indexer
# 'indexer' has dupes, create 'check' using 'missing'
check = np.zeros_like(objarr)
check[missing] = -1

mask = check == -1
if mask.any():
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,15 @@ def f():
self.assertEqual(result, 3)
self.assertRaises(ValueError, lambda: df.at['a', 0])

# GH 13822, incorrect error string with non-unique columns when missing
# column is accessed
df = DataFrame({'x': [1.], 'y': [2.], 'z': [3.]})
df.columns = ['x', 'x', 'z']

# Check that we get the correct value in the KeyError
self.assertRaisesRegexp(KeyError, "\['y'\] not in index",
lambda: df[['x', 'y', 'z']])

def test_loc_getitem_label_slice(self):

# label slices (with ints)
Expand Down