Skip to content

Let MultiIndex accept a list-like as input #23887

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

Open
topper-123 opened this issue Nov 24, 2018 · 6 comments
Open

Let MultiIndex accept a list-like as input #23887

topper-123 opened this issue Nov 24, 2018 · 6 comments
Labels
Constructors Series/DataFrame/Index/pd.array Constructors Enhancement MultiIndex Needs Discussion Requires discussion from core team before further action

Comments

@topper-123
Copy link
Contributor

topper-123 commented Nov 24, 2018

In #22511 and #23752 I work on some changes to MultiIndex. It came up in discussion that the consteuction of MultiIndex shold be changed, so that initialising with a list of tuples should be accepted:

>>>pd.MultiIndex([('a', 1)
...               ('b', 2)])
MultiIndex([('a', 1)
            ('b', 2)])

I'm also thinking that MultiIndex could accept dicts when initialising, and it would work similarly, except the dict keys would become the values for MultiIndex.names, and the dict values would be level values for the MultiIndex.

@topper-123 topper-123 changed the title LEt MultiIndex ax Let MultiIndex accept a list-like as input Nov 24, 2018
@ms7463
Copy link
Contributor

ms7463 commented Nov 25, 2018

The arrays/dictionary initialization would be covered in the from_data proposal from this PR: #23141. Although pd.MultiIndex.from_arrays/from_tuples would already cover list-like input.

@jreback / @toobaz / @TomAugspurger - does this add any merit to from_data over the dedicated from_frame?

@gfyoung
Copy link
Member

gfyoung commented Nov 26, 2018

I'm confused here...don't we already have MultiIndex.from_tuples ? Are we just integrating that behavior directly into the constructor?

@jreback
Copy link
Contributor

jreback commented Nov 26, 2018

the proposal to make the default constructor to this

@topper-123
Copy link
Contributor Author

@gfyoung , Yes, basically the proposal is to default constructor accept the arguments from from_tuples + plus others, so this should be fully backwards compatible.

@gfyoung
Copy link
Member

gfyoung commented Nov 28, 2018

@topper-123 : Yep, that makes sense. +1 from me.

@shoyer
Copy link
Member

shoyer commented Dec 9, 2018

I don't think this makes sense. The default pd.MultiIndex constructor intentionally mirrors the under-lying data-structure (in terms of levels and codes). It's OK if users don't use the constructor directly -- we can aways make convenience functions for that (which indeed already exist). We could even make a new "try everything" converter function pd.to_multiindex(), though I'm not sure how you could reliably distinguish from_tuples from from_arrays.

There is no iron-clad rule that a class's repr needs to use its constructor directly -- we could make the repr show MultiIndex.from_tuples(...) if desired. The situation is pretty similar to that for np.ndarray (with its low-level constructor) and np.array (which is used in the repr).

If you did go ahead with this, what would you do with the existing constructor? Would you delegate the existing constructor to another method, or would you simply overload the first argument levels to check for a list of tuples? This sort of heavy overloading makes code harder to understand and error check (either manually or with static checks like mypy/pylint).

@jbrockmendel jbrockmendel added the Constructors Series/DataFrame/Index/pd.array Constructors label Sep 21, 2020
@mroeschke mroeschke added Enhancement Needs Discussion Requires discussion from core team before further action and removed API Design labels Jun 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Constructors Series/DataFrame/Index/pd.array Constructors Enhancement MultiIndex Needs Discussion Requires discussion from core team before further action
Projects
None yet
Development

No branches or pull requests

7 participants