-
-
Notifications
You must be signed in to change notification settings - Fork 18.8k
BUG: set_levels set illegal levels. #14236
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
4093ff9
fbace39
6e56ff3
40bcdc7
be8dfba
30bbeb4
e1a6e0a
005ab51
39b7bde
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
set_levels
set illegal levels.
`MultiIndex.set_levels`, when passed in illegal level values, raises an error. When `inplace=True`, though, the illegal level values are still accepted. This commit fixes that behavior by checking that the proposed level values are legal before setting them. added tests and docs. lint added tests. force build force build force build force build for osx
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,12 +116,22 @@ def __new__(cls, levels=None, labels=None, sortorder=None, names=None, | |
|
||
return result | ||
|
||
def _verify_integrity(self): | ||
def _verify_integrity(self, new_labels=None, new_levels=None): | ||
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. call these 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. Fixed. |
||
"""Raises ValueError if length of levels and labels don't match or any | ||
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 |
||
label would exceed level bounds""" | ||
label would exceed level bounds | ||
|
||
Parameters | ||
---------- | ||
new_labels : optional list | ||
Labels to check for validity. Defaults to current labels. | ||
new_levels : optional list | ||
Levels to check for validity. Defaults to current levels. | ||
""" | ||
# NOTE: Currently does not check, among other things, that cached | ||
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 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. Fixed. |
||
# nlevels matches nor that sortorder matches actually sortorder. | ||
labels, levels = self.labels, self.levels | ||
labels = new_labels or self.labels | ||
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 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. Ah, looks like it'll be a |
||
levels = new_levels or self.levels | ||
|
||
if len(levels) != len(labels): | ||
raise ValueError("Length of levels and labels must match. NOTE:" | ||
" this index is in an inconsistent state.") | ||
|
@@ -162,6 +172,9 @@ def _set_levels(self, levels, level=None, copy=False, validate=True, | |
new_levels[l] = _ensure_index(v, copy=copy)._shallow_copy() | ||
new_levels = FrozenList(new_levels) | ||
|
||
if verify_integrity: | ||
self._verify_integrity(new_levels=new_levels) | ||
|
||
names = self.names | ||
self._levels = new_levels | ||
if any(names): | ||
|
@@ -170,9 +183,6 @@ def _set_levels(self, levels, level=None, copy=False, validate=True, | |
self._tuples = None | ||
self._reset_cache() | ||
|
||
if verify_integrity: | ||
self._verify_integrity() | ||
|
||
def set_levels(self, levels, level=None, inplace=False, | ||
verify_integrity=True): | ||
""" | ||
|
@@ -268,13 +278,13 @@ def _set_labels(self, labels, level=None, copy=False, validate=True, | |
lab, lev, copy=copy)._shallow_copy() | ||
new_labels = FrozenList(new_labels) | ||
|
||
if verify_integrity: | ||
self._verify_integrity(new_labels=new_labels) | ||
|
||
self._labels = new_labels | ||
self._tuples = None | ||
self._reset_cache() | ||
|
||
if verify_integrity: | ||
self._verify_integrity() | ||
|
||
def set_labels(self, labels, level=None, inplace=False, | ||
verify_integrity=True): | ||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -154,7 +154,7 @@ def assert_matching(actual, expected): | |
# as much as possible | ||
self.assertEqual(len(actual), len(expected)) | ||
for act, exp in zip(actual, expected): | ||
act = np.asarray(act) | ||
act = np.asarray(act, dtype=np.object_) | ||
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. why did you need to change this? 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. Because the expected is changed to 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 show me an example? it is passing now, so unclear why you had to change it. 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. Just pushed up commit e0c8897 that undoes this change and fails with the error:
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. int8 is right 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. But when I take out the coercion to
This is in the test
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. It looks like maybe 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. Fixed this issue. |
||
exp = np.asarray(exp, dtype=np.object_) | ||
tm.assert_numpy_array_equal(act, exp) | ||
|
||
|
@@ -204,6 +204,25 @@ def assert_matching(actual, expected): | |
assert_matching(ind2.levels, new_levels) | ||
assert_matching(self.index.levels, levels) | ||
|
||
# illegal level changing should not change levels | ||
# GH 13754 | ||
original_index = self.index.copy() | ||
with assertRaisesRegexp(ValueError, "^On"): | ||
self.index.set_levels(['c'], level=0, inplace=True) | ||
assert_matching(self.index.levels, original_index.levels) | ||
|
||
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 test |
||
with assertRaisesRegexp(ValueError, "^On"): | ||
self.index.set_labels([0, 1, 2, 3, 4, 5], level=0, inplace=True) | ||
assert_matching(self.index.labels, original_index.labels) | ||
|
||
with assertRaisesRegexp(TypeError, "^Levels"): | ||
self.index.set_levels('c', level=0, inplace=True) | ||
assert_matching(self.index.levels, original_index.levels) | ||
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 tests for |
||
|
||
with assertRaisesRegexp(TypeError, "^Labels"): | ||
self.index.set_labels(1, level=0, inplace=True) | ||
assert_matching(self.index.labels, original_index.labels) | ||
|
||
def test_set_labels(self): | ||
# side note - you probably wouldn't want to use levels and labels | ||
# directly like this - but it is possible. | ||
|
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.
move to 0.19.1
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.
Moved.