-
-
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
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 |
---|---|---|
|
@@ -672,6 +672,7 @@ def test_from_arrays(self): | |
for lev, lab in zip(self.index.levels, self.index.labels): | ||
arrays.append(np.asarray(lev).take(lab)) | ||
|
||
# list of arrays as input | ||
result = MultiIndex.from_arrays(arrays) | ||
assert list(result) == list(self.index) | ||
|
||
|
@@ -681,6 +682,21 @@ def test_from_arrays(self): | |
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)) | ||
assert list(result) == list(self.index) | ||
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. use |
||
|
||
# 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') | ||
|
@@ -827,6 +843,26 @@ def test_from_product(self): | |
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 | ||
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 make this a separate test (the added code), and do so for from_product, from_tuples, from_arrays as well, call it something like
|
||
result = MultiIndex.from_product(iter([first, second]), names=names) | ||
assert result.equals(expected) | ||
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. use assert_index_equal |
||
assert result.names == names | ||
|
||
# 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 | ||
with tm.assert_raises_regex( | ||
|
@@ -1725,8 +1761,30 @@ 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 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']) | ||
assert expected.names == result.names | ||
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 |
||
assert result.equals(expected) | ||
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 |
||
|
||
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']) | ||
assert expected.names == result.names | ||
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 |
||
assert result.equals(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 | ||
|
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