-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
API: disallow duplicate level names #18882
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 all commits
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 |
---|---|---|
|
@@ -536,15 +536,6 @@ def test_names(self): | |
level_names = [level.name for level in index.levels] | ||
assert ind_names == level_names | ||
|
||
def test_reference_duplicate_name(self): | ||
idx = MultiIndex.from_tuples( | ||
[('a', 'b'), ('c', 'd')], names=['x', 'x']) | ||
assert idx._reference_duplicate_name('x') | ||
|
||
idx = MultiIndex.from_tuples( | ||
[('a', 'b'), ('c', 'd')], names=['x', 'y']) | ||
assert not idx._reference_duplicate_name('x') | ||
|
||
def test_astype(self): | ||
expected = self.index.copy() | ||
actual = self.index.astype('O') | ||
|
@@ -609,6 +600,23 @@ def test_constructor_mismatched_label_levels(self): | |
with tm.assert_raises_regex(ValueError, label_error): | ||
self.index.copy().set_labels([[0, 0, 0, 0], [0, 0]]) | ||
|
||
@pytest.mark.parametrize('names', [['a', 'b', 'a'], [1, 1, 2], | ||
[1, 'a', 1]]) | ||
def test_duplicate_level_names(self, names): | ||
# GH18872 | ||
pytest.raises(ValueError, pd.MultiIndex.from_product, | ||
[[0, 1]] * 3, names=names) | ||
|
||
# With .rename() | ||
mi = pd.MultiIndex.from_product([[0, 1]] * 3) | ||
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 check the error messages here, use 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) |
||
tm.assert_raises_regex(ValueError, "Duplicated level name:", | ||
mi.rename, names) | ||
|
||
# With .rename(., level=) | ||
mi.rename(names[0], level=1, inplace=True) | ||
tm.assert_raises_regex(ValueError, "Duplicated level name:", | ||
mi.rename, names[:2], level=[0, 2]) | ||
|
||
def assert_multiindex_copied(self, copy, original): | ||
# Levels should be (at least, shallow copied) | ||
tm.assert_copy(copy.levels, original.levels) | ||
|
@@ -667,11 +675,6 @@ def test_changing_names(self): | |
shallow_copy.names = [name + "c" for name in shallow_copy.names] | ||
self.check_level_names(self.index, new_names) | ||
|
||
def test_duplicate_names(self): | ||
self.index.names = ['foo', 'foo'] | ||
tm.assert_raises_regex(KeyError, 'Level foo not found', | ||
self.index._get_level_number, 'foo') | ||
|
||
def test_get_level_number_integer(self): | ||
self.index.names = [1, 0] | ||
assert self.index._get_level_number(1) == 0 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -214,17 +214,6 @@ def test_reorder_levels(self): | |
expected = Series(np.arange(6), index=e_idx) | ||
assert_series_equal(result, expected) | ||
|
||
result = s.reorder_levels([0, 0, 0]) | ||
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 test that raises 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 is the new |
||
e_idx = MultiIndex(levels=[['bar'], ['bar'], ['bar']], | ||
labels=[[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], | ||
[0, 0, 0, 0, 0, 0]], | ||
names=['L0', 'L0', 'L0']) | ||
expected = Series(np.arange(6), index=e_idx) | ||
assert_series_equal(result, expected) | ||
|
||
result = s.reorder_levels(['L0', 'L0', 'L0']) | ||
assert_series_equal(result, expected) | ||
|
||
def test_rename_axis_inplace(self): | ||
# GH 15704 | ||
series = self.ts.copy() | ||
|
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.
what does this do now?
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.
It raises when you try to create the
MultiIndex
with duplicated name.