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

DataFrame.stack() raises TypeError on mixed type level under Python 3 #18310

Closed
toobaz opened this Issue Nov 15, 2017 · 1 comment

Comments

Projects
None yet
3 participants
@toobaz
Member

toobaz commented Nov 15, 2017

Code Sample, a copy-pastable example if possible

In [2]: df = pd.DataFrame(1, index=range(3), columns=pd.MultiIndex.from_product([['a', 'b'], [1, 2]]))

In [3]: df['c'] = 2

In [4]: df.stack(0)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-a437419299b6> in <module>()
----> 1 df.stack(0)

/home/nobackup/repo/pandas/pandas/core/frame.py in stack(self, level, dropna)
   4500             return stack_multiple(self, level, dropna=dropna)
   4501         else:
-> 4502             return stack(self, level, dropna=dropna)
   4503 
   4504     def unstack(self, level=-1, fill_value=None):

/home/nobackup/repo/pandas/pandas/core/reshape/reshape.py in stack(frame, level, dropna)
    513 
    514     if isinstance(frame.columns, MultiIndex):
--> 515         return _stack_multi_columns(frame, level_num=level_num, dropna=dropna)
    516     elif isinstance(frame.index, MultiIndex):
    517         new_levels = list(frame.index.levels)

/home/nobackup/repo/pandas/pandas/core/reshape/reshape.py in _stack_multi_columns(frame, level_num, dropna)
    619         # level
    620         level_to_sort = _convert_level_number(0, this.columns)
--> 621         this = this.sort_index(level=level_to_sort, axis=1)
    622 
    623     # tuple list excluding level for grouping columns

/home/nobackup/repo/pandas/pandas/core/frame.py in sort_index(self, axis, level, ascending, inplace, kind, na_position, sort_remaining, by)
   3673             # make sure that the axis is lexsorted to start
   3674             # if not we need to reconstruct to get the correct indexer
-> 3675             labels = labels._sort_levels_monotonic()
   3676             indexer = lexsort_indexer(labels._get_labels_for_sorting(),
   3677                                       orders=ascending,

/home/nobackup/repo/pandas/pandas/core/indexes/multi.py in _sort_levels_monotonic(self)
   1275 
   1276             # indexer to reorder the levels
-> 1277             indexer = lev.argsort()
   1278             lev = lev.take(indexer)
   1279 

/home/nobackup/repo/pandas/pandas/core/indexes/base.py in argsort(self, *args, **kwargs)
   2146         if result is None:
   2147             result = np.array(self)
-> 2148         return result.argsort(*args, **kwargs)
   2149 
   2150     def __add__(self, other):

TypeError: unorderable types: str() > int()

Problem description

This is even more confusing because df[['a', 'b']].stack(0) (where no mixed dtype is left) results in the same error.

Expected Output

In [13]: df.stack(0)
Out[13]: 
       1    2    
0 a  1.0  1.0  NaN
  b  1.0  1.0  NaN
  c  NaN  NaN  2.0
1 a  1.0  1.0  NaN
  b  1.0  1.0  NaN
  c  NaN  NaN  2.0
2 a  1.0  1.0  NaN
  b  1.0  1.0  NaN
  c  NaN  NaN  2.0

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]

@toobaz toobaz changed the title from DataFrame.stack() raises TypeError on mixed type level to DataFrame.stack() raises TypeError on mixed type level under Python 3 Nov 15, 2017

@gfyoung

This comment has been minimized.

Member

gfyoung commented Nov 16, 2017

Very odd indeed. Looking at the stacktrace, what is the value of result when the code breaks? Probably a good starting point to find the bug and patch.

toobaz added a commit to toobaz/pandas that referenced this issue Nov 19, 2017

@jreback jreback added this to the 0.22.0 milestone Nov 19, 2017

toobaz added a commit to toobaz/pandas that referenced this issue Nov 19, 2017

jreback added a commit that referenced this issue Dec 1, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment