-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: MultiIndex truncated for some non-list arguments #14806
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 |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
import pandas.index as _index | ||
from pandas.lib import Timestamp | ||
|
||
from pandas.compat import range, zip, lrange, lzip, map | ||
from pandas.compat import range, zip, lrange, lzip, map, zip_longest, ABC | ||
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 import directly from itertools 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. zip_longest changed names between Python 2/3 (it was izip_longest in Python 2) |
||
from pandas.compat.numpy import function as nv | ||
from pandas import compat | ||
|
||
|
@@ -916,19 +916,24 @@ def from_arrays(cls, arrays, sortorder=None, names=None): | |
|
||
See Also | ||
-------- | ||
MultiIndex.from_tuples : Convert list of tuples to MultiIndex | ||
MultiIndex.from_tuples : Convert sequence of tuples to MultiIndex | ||
MultiIndex.from_product : Make a MultiIndex from cartesian product | ||
of iterables | ||
""" | ||
try: | ||
arrays = list(arrays) | ||
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. revert |
||
except: | ||
raise ValueError('arrays must be iterable/sequence') | ||
|
||
if len(arrays) == 1: | ||
name = None if names is None else names[0] | ||
return Index(arrays[0], name=name) | ||
|
||
# Check if lengths of all arrays are equal or not, | ||
# raise ValueError, if not | ||
for i in range(1, len(arrays)): | ||
if len(arrays[i]) != len(arrays[i - 1]): | ||
raise ValueError('all arrays must be same length') | ||
_len_first = len(arrays[0]) | ||
if not all(len(x) == _len_first for x in arrays): | ||
raise ValueError('all arrays must be same length') | ||
|
||
from pandas.core.categorical import _factorize_from_iterables | ||
|
||
|
@@ -942,7 +947,7 @@ def from_arrays(cls, arrays, sortorder=None, names=None): | |
@classmethod | ||
def from_tuples(cls, tuples, sortorder=None, names=None): | ||
""" | ||
Convert list of tuples to MultiIndex | ||
Convert sequence of tuples to MultiIndex | ||
|
||
Parameters | ||
---------- | ||
|
@@ -964,24 +969,18 @@ def from_tuples(cls, tuples, sortorder=None, names=None): | |
|
||
See Also | ||
-------- | ||
MultiIndex.from_arrays : Convert list of arrays to MultiIndex | ||
MultiIndex.from_arrays : Convert sequence of arrays to MultiIndex | ||
MultiIndex.from_product : Make a MultiIndex from cartesian product | ||
of iterables | ||
""" | ||
if len(tuples) == 0: | ||
# I think this is right? Not quite sure... | ||
raise TypeError('Cannot infer number of levels from empty list') | ||
|
||
if isinstance(tuples, (np.ndarray, Index)): | ||
if isinstance(tuples, Index): | ||
tuples = tuples._values | ||
if isinstance(tuples, Index): | ||
tuples = tuples._values | ||
|
||
arrays = list(lib.tuples_to_object_array(tuples).T) | ||
elif isinstance(tuples, list): | ||
arrays = list(lib.to_object_array_tuples(tuples).T) | ||
if isinstance(tuples, np.ndarray): | ||
arrays = lib.tuples_to_object_array(tuples).T | ||
else: | ||
arrays = lzip(*tuples) | ||
|
||
arrays = zip_longest(*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. revert all except for this change here |
||
return MultiIndex.from_arrays(arrays, sortorder=sortorder, names=names) | ||
|
||
@classmethod | ||
|
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.
revert all this. way out of scope for this issue.