-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: Introduce UnsortedIndexError GH11897 #14762
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,6 +97,17 @@ class UnsupportedFunctionCall(ValueError): | |
pass | ||
|
||
|
||
class UnsortedIndexError(KeyError): | ||
""" Error raised when attempting to get a slice of a MultiIndex | ||
and the index has not been lexsorted. Subclass of `KeyError`. | ||
|
||
.. versionadded:: 0.20.0 | ||
|
||
""" | ||
pass | ||
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. can you add a doc-string 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. done |
||
|
||
|
||
|
||
class AbstractMethodError(NotImplementedError): | ||
"""Raise this error instead of NotImplementedError for abstract methods | ||
while keeping compatibility with Python 2 and Python 3. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ | |
|
||
from pandas import (DataFrame, date_range, period_range, MultiIndex, Index, | ||
CategoricalIndex, compat) | ||
from pandas.core.common import PerformanceWarning | ||
from pandas.core.common import PerformanceWarning, UnsortedIndexError | ||
from pandas.indexes.base import InvalidIndexError | ||
from pandas.compat import range, lrange, u, PY3, long, lzip | ||
|
||
|
@@ -2535,3 +2535,19 @@ def test_dropna(self): | |
msg = "invalid how option: xxx" | ||
with tm.assertRaisesRegexp(ValueError, msg): | ||
idx.dropna(how='xxx') | ||
|
||
def test_unsortedindex(self): | ||
# GH 11897 | ||
mi = pd.MultiIndex.from_tuples([('z', 'a'), ('x', 'a'), ('y', 'b'), | ||
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. we want to find places where currently a KeyError is raised and change them in the tests. 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. I've now done that. My way of doing that was to make UnsortedIndexError a subclass of ValueError, run nosetests to see where it complained, and then change those tests accordingly. Then I switched UnsortedIndexError back to being a subclass of KeyError. |
||
('x', 'b'), ('y', 'a'), ('z', 'b')], | ||
names=['one', 'two']) | ||
df = pd.DataFrame([[i, 10 * i] for i in lrange(6)], index=mi, | ||
columns=['one', 'two']) | ||
|
||
with assertRaises(UnsortedIndexError): | ||
df.loc(axis=0)['z', :] | ||
df.sort_index(inplace=True) | ||
self.assertEqual(len(df.loc(axis=0)['z', :]), 2) | ||
|
||
with assertRaises(KeyError): | ||
df.loc(axis=0)['q', :] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
MultiIndex, Timestamp, Timedelta) | ||
from pandas.formats.printing import pprint_thing | ||
from pandas import concat | ||
from pandas.core.common import PerformanceWarning | ||
from pandas.core.common import PerformanceWarning, UnsortedIndexError | ||
|
||
import pandas.util.testing as tm | ||
from pandas import date_range | ||
|
@@ -2230,7 +2230,7 @@ def f(): | |
df = df.sortlevel(level=1, axis=0) | ||
self.assertEqual(df.index.lexsort_depth, 0) | ||
with tm.assertRaisesRegexp( | ||
KeyError, | ||
UnsortedIndexError, | ||
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. hmm only these 2 occurences of the original KeyError.....? 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. Via my methodology described above, that's all I found. |
||
'MultiIndex Slicing requires the index to be fully ' | ||
r'lexsorted tuple len \(2\), lexsort depth \(0\)'): | ||
df.loc[(slice(None), df.loc[:, ('a', 'bar')] > 5), :] | ||
|
@@ -2417,7 +2417,7 @@ def test_per_axis_per_level_doc_examples(self): | |
def f(): | ||
df.loc['A1', (slice(None), 'foo')] | ||
|
||
self.assertRaises(KeyError, f) | ||
self.assertRaises(UnsortedIndexError, f) | ||
df = df.sortlevel(axis=1) | ||
|
||
# slicing | ||
|
@@ -3480,8 +3480,12 @@ def test_iloc_mask(self): | |
('index', '.loc'): '0b11', | ||
('index', '.iloc'): ('iLocation based boolean indexing ' | ||
'cannot use an indexable as a mask'), | ||
('locs', ''): 'Unalignable boolean Series key provided', | ||
('locs', '.loc'): 'Unalignable boolean Series key provided', | ||
('locs', ''): 'Unalignable boolean Series provided as indexer ' | ||
'(index of the boolean Series and of the indexed ' | ||
'object do not match', | ||
('locs', '.loc'): 'Unalignable boolean Series provided as indexer ' | ||
'(index of the boolean Series and of the indexed ' | ||
'object do not match', | ||
('locs', '.iloc'): ('iLocation based boolean indexing on an ' | ||
'integer type is not available'), | ||
} | ||
|
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.
add a versiononadded tag here
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.
"This error is raised when" -> "Error raised when"
Can you also add that it is a subclass of a KeyError?
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.
done