def stack(frame, level=-<NUM_LIT:1>, dropna=True):
def factorize(index):<EOL><INDENT>if index.is_unique:<EOL><INDENT>return index, np.arange(len(index))<EOL><DEDENT>codes, categories = _factorize_from_iterable(index)<EOL>return categories, codes<EOL><DEDENT>N, K = frame.shape<EOL>level_num = frame.columns._get_level_number(level)<EOL>if isinstance(frame.columns, MultiIndex):<EOL><INDENT>return _stack_multi_columns(frame, level_num=level_num, dropna=dropna)<EOL><DEDENT>elif isinstance(frame.index, MultiIndex):<EOL><INDENT>new_levels = list(frame.index.levels)<EOL>new_codes = [lab.repeat(K) for lab in frame.index.codes]<EOL>clev, clab = factorize(frame.columns)<EOL>new_levels.append(clev)<EOL>new_codes.append(np.tile(clab, N).ravel())<EOL>new_names = list(frame.index.names)<EOL>new_names.append(frame.columns.name)<EOL>new_index = MultiIndex(levels=new_levels, codes=new_codes,<EOL>names=new_names, verify_integrity=False)<EOL><DEDENT>else:<EOL><INDENT>levels, (ilab, clab) = zip(*map(factorize, (frame.index,<EOL>frame.columns)))<EOL>codes = ilab.repeat(K), np.tile(clab, N).ravel()<EOL>new_index = MultiIndex(levels=levels, codes=codes,<EOL>names=[frame.index.name, frame.columns.name],<EOL>verify_integrity=False)<EOL><DEDENT>if frame._is_homogeneous_type:<EOL><INDENT>dtypes = list(frame.dtypes.values)<EOL>dtype = dtypes[<NUM_LIT:0>]<EOL>if is_extension_array_dtype(dtype):<EOL><INDENT>arr = dtype.construct_array_type()<EOL>new_values = arr._concat_same_type([<EOL>col._values for _, col in frame.iteritems()<EOL>])<EOL>new_values = _reorder_for_extension_array_stack(new_values, N, K)<EOL><DEDENT>else:<EOL><INDENT>new_values = frame.values.ravel()<EOL><DEDENT><DEDENT>else:<EOL><INDENT>new_values = frame.values.ravel()<EOL><DEDENT>if dropna:<EOL><INDENT>mask = notna(new_values)<EOL>new_values = new_values[mask]<EOL>new_index = new_index[mask]<EOL><DEDENT>return frame._constructor_sliced(new_values, index=new_index)<EOL>
Convert DataFrame to Series with multi-level Index. Columns become the
second level of the resulting hierarchical index

Returns
-------
stacked : Series