Skip to content

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions pandas/compat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,8 @@ def set_function_name(f, name, cls):
return f

ResourceWarning = ResourceWarning

zip_longest = itertools.zip_longest

else:
string_types = basestring,
Expand Down Expand Up @@ -309,9 +311,30 @@ def set_function_name(f, name, cls):

class ResourceWarning(Warning): pass

zip_longest = itertools.izip_longest

string_and_binary_types = string_types + (binary_type,)


class ABC(object):
Copy link
Contributor

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.

if PY3:
from collections import abc
Container = abc.Container
Iterable = abc.Iterable
Iterator = abc.Iterator
Sequence = abc.Sequence
MutableSequence = abc.MutableSequence
Mapping = abc.Mapping
else:
import collections
Container = collections.Container
Iterable = collections.Iterable
Iterator = collections.Iterator
Sequence = collections.Sequence
MutableSequence = collections.MutableSequence
Mapping = collections.Mapping


try:
# callable reintroduced in later versions of Python
callable = callable
Expand Down
37 changes: 18 additions & 19 deletions pandas/indexes/multi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just import directly from itertools

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Expand All @@ -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
----------
Expand All @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand Down