-
-
Notifications
You must be signed in to change notification settings - Fork 18k
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
BUG: in Python3 MultiIndex.from_tuples cannot take "zipped" tuples #18440
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 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 |
---|---|---|
|
@@ -672,15 +672,31 @@ def test_from_arrays(self): | |
for lev, lab in zip(self.index.levels, self.index.labels): | ||
arrays.append(np.asarray(lev).take(lab)) | ||
|
||
result = MultiIndex.from_arrays(arrays) | ||
assert list(result) == list(self.index) | ||
# list of arrays as input | ||
result = MultiIndex.from_arrays(arrays, names=self.index.names) | ||
tm.assert_index_equal(result, self.index) | ||
|
||
# infer correctly | ||
result = MultiIndex.from_arrays([[pd.NaT, Timestamp('20130101')], | ||
['a', 'b']]) | ||
assert result.levels[0].equals(Index([Timestamp('20130101')])) | ||
assert result.levels[1].equals(Index(['a', 'b'])) | ||
|
||
def test_from_arrays_iterator(self): | ||
# GH 18434 | ||
arrays = [] | ||
for lev, lab in zip(self.index.levels, self.index.labels): | ||
arrays.append(np.asarray(lev).take(lab)) | ||
|
||
# iterator as input | ||
result = MultiIndex.from_arrays(iter(arrays), names=self.index.names) | ||
tm.assert_index_equal(result, self.index) | ||
|
||
# invalid iterator input | ||
with tm.assert_raises_regex( | ||
TypeError, "Input must be a list / sequence of array-likes."): | ||
MultiIndex.from_arrays(0) | ||
|
||
def test_from_arrays_index_series_datetimetz(self): | ||
idx1 = pd.date_range('2015-01-01 10:00', freq='D', periods=3, | ||
tz='US/Eastern') | ||
|
@@ -825,7 +841,25 @@ def test_from_product(self): | |
expected = MultiIndex.from_tuples(tuples, names=names) | ||
|
||
tm.assert_index_equal(result, expected) | ||
assert result.names == names | ||
|
||
def test_from_product_iterator(self): | ||
# GH 18434 | ||
first = ['foo', 'bar', 'buz'] | ||
second = ['a', 'b', 'c'] | ||
names = ['first', 'second'] | ||
tuples = [('foo', 'a'), ('foo', 'b'), ('foo', 'c'), ('bar', 'a'), | ||
('bar', 'b'), ('bar', 'c'), ('buz', 'a'), ('buz', 'b'), | ||
('buz', 'c')] | ||
expected = MultiIndex.from_tuples(tuples, names=names) | ||
|
||
# iterator as input | ||
result = MultiIndex.from_product(iter([first, second]), names=names) | ||
tm.assert_index_equal(result, expected) | ||
|
||
# Invalid non-iterable input | ||
with tm.assert_raises_regex( | ||
TypeError, "Input must be a list / sequence of iterables."): | ||
MultiIndex.from_product(0) | ||
|
||
def test_from_product_empty(self): | ||
# 0 levels | ||
|
@@ -1725,8 +1759,28 @@ def test_from_tuples(self): | |
'from empty list', | ||
MultiIndex.from_tuples, []) | ||
|
||
idx = MultiIndex.from_tuples(((1, 2), (3, 4)), names=['a', 'b']) | ||
assert len(idx) == 2 | ||
expected = MultiIndex(levels=[[1, 3], [2, 4]], | ||
labels=[[0, 1], [0, 1]], | ||
names=['a', 'b']) | ||
|
||
# input tuples | ||
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. same (new test) |
||
result = MultiIndex.from_tuples(((1, 2), (3, 4)), names=['a', 'b']) | ||
tm.assert_index_equal(result, expected) | ||
|
||
def test_from_tuples_iterator(self): | ||
# GH 18434 | ||
# input iterator for tuples | ||
expected = MultiIndex(levels=[[1, 3], [2, 4]], | ||
labels=[[0, 1], [0, 1]], | ||
names=['a', 'b']) | ||
|
||
result = MultiIndex.from_tuples(zip([1, 3], [2, 4]), names=['a', 'b']) | ||
tm.assert_index_equal(result, expected) | ||
|
||
# input non-iterables | ||
with tm.assert_raises_regex( | ||
TypeError, 'Input must be a list / sequence of tuple-likes.'): | ||
MultiIndex.from_tuples(0) | ||
|
||
def test_from_tuples_empty(self): | ||
# GH 16777 | ||
|
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.
same