Skip to content
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

Feature Request: pd.MultiIndex.from_frame(). Complement to pd.MultiIndex.to_frame(). #22420

Closed
ArtinSarraf opened this issue Aug 19, 2018 · 3 comments

Comments

Projects
None yet
3 participants
@ArtinSarraf
Copy link
Contributor

commented Aug 19, 2018

The MultiIndex.to_frame function is great for working with multi-indexes as a meta-dataframe. I find myself using this paradigm very often.

df = create_multiindex_df()
meta = df.columns.to_frame(index=False)
meta = filter_multiindex()
df.reindex(columns=convert_df_to_multiindex(df))

Having the convert_df_to_multiindex as a pd.MultiIndex method would be extremely complimentary to pd.MultiIndex.to_frame.

A simplified one-line implementation provided below, not including accounting for some corner case behaviors and discerning between series/frames vs Index/Multiindex.

pd.MultiIndex.from_tuples(list(df.values), names=df.columns)
@jreback

This comment has been minimized.

Copy link
Contributor

commented Aug 19, 2018

can u show a complete example

@ArtinSarraf

This comment has been minimized.

Copy link
Contributor Author

commented Aug 19, 2018

Here is a more complete implementation:

# pd.Index

@classmethod
def from_frame(cls, df):
    if not isinstance(df.squeeze(), pd.Series):
        raise ValueError('DataFrame must be be single column')
    return cls.from_series(df.squeeze())


@classmethod
def from_series(cls, s):
    return cls(s, name=s.name)



# pd.MultiIndex

@classmethod
def from_frame(cls, df, squeeze=True):
    """
    :param df
    :param squeeze
        Squeeze single level multiindex to be a regular index
    """
    # just let column level names be the tuple of the meta df columns since they're not required to be strings
    # columns = ['.'.join(col) for col in list(df)]  
    columns = list(df)
    mi = cls.from_tuples(list(df.values), names=columns)
    if squeeze:
        if len(mi.levels) == 1:
            return mi.levels[0][mi.labels[0]]
        else:
            return mi
    else:
        return mi

@ArtinSarraf ArtinSarraf referenced this issue Oct 13, 2018

Merged

ENH: MultiIndex.from_frame #23141

3 of 4 tasks complete
@ArtinSarraf

This comment has been minimized.

Copy link
Contributor Author

commented Nov 1, 2018

Please see this comment for a justification of this feature.

Please see this comment for a demonstration of the new vs current way to do specific tasks.

@jreback jreback added this to the 0.24.0 milestone Dec 4, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.