From a60467d83a391612f288b247567b1f4dd183b415 Mon Sep 17 00:00:00 2001 From: Xbar Date: Wed, 22 Nov 2017 18:41:02 -0800 Subject: [PATCH] Fix issue 18434 MultiIndex.from_tuples accept zipped tuples in python 3. Compatibility between 2 and 3. --- doc/source/whatsnew/v0.22.0.txt | 1 + pandas/core/indexes/multi.py | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/doc/source/whatsnew/v0.22.0.txt b/doc/source/whatsnew/v0.22.0.txt index 4a4d60b4dfbb29..533ecd19e4ef4e 100644 --- a/doc/source/whatsnew/v0.22.0.txt +++ b/doc/source/whatsnew/v0.22.0.txt @@ -137,6 +137,7 @@ Indexing - Bug in :func:`Series.truncate` which raises ``TypeError`` with a monotonic ``PeriodIndex`` (:issue:`17717`) - Bug in :func:`DataFrame.groupby` where tuples were interpreted as lists of keys rather than as keys (:issue:`17979`, :issue:`18249`) - Bug in :func:`MultiIndex.remove_unused_levels`` which would fill nan values (:issue:`18417`) +- Bug in :func:`MultiIndex.from_tuples`` which would fail to take zipped tuples in python3 (:issue:`18434`) - I/O diff --git a/pandas/core/indexes/multi.py b/pandas/core/indexes/multi.py index cc99505b53bf5d..d1d392a9499552 100644 --- a/pandas/core/indexes/multi.py +++ b/pandas/core/indexes/multi.py @@ -1206,6 +1206,12 @@ def from_tuples(cls, tuples, sortorder=None, names=None): MultiIndex.from_product : Make a MultiIndex from cartesian product of iterables """ + if not hasattr(tuples, '__len__'): + try: + iter(tuples) # if iterable, then it's iterator or generator + except TypeError: + raise TypeError('Inappropriate input for tuples.') + tuples = list(tuples) if len(tuples) == 0: if names is None: msg = 'Cannot infer number of levels from empty list'