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

ENH Add MultiIndex.from_product convenience function #6055

Merged

Conversation

mwaskom
Copy link
Contributor

@mwaskom mwaskom commented Jan 24, 2014

Following up on this StackOverflow post.

This is a convenience function for when you have a set of iterables and you want to make a MultiIndex with each unique paring of the values in those iterables.

I'm testing it against itertools.product since it uses the internal cartesian_product function; please let me know if there are additional tests that would make sense.

Testing against MultiIndex.from_tuples instead.

result = MultiIndex.from_iterables([first, second], names=names)

product = list(itertools.product(first, second))
self.assertEquals(list(result), product)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use assert_array_equals here
also test that this equals to a constructed instance using MultiIndex.from_tuples (but construct if by writing out the tuples)
eg make it so it doesn't depend on iter tools or other routines)
that was if some ordering changes u are covered

  • add a release notes entry in improvements section
  • an example in v0.13.1
  • same example in the multi-index construction section in main docs (eg where Multiindex.from_tuples is introduced)

@jorisvandenbossche
Copy link
Member

I have a remark about this. First, it's really usefull functionality! (it's indeed a common usecase)

But the name sounds confusing to me. Because it seems not that important that it are iterables, but that it's from a cross combination of two iterables (you can also use iterables in from_arrays/from_tuples). For me it's not obvious that if you have iterables that you would logically take a product of it.
But I admit I don't directly have that clear or short alternatives (from_combination, from_product, from_iter_product, from_cartesias_product, ..)

@jreback
Copy link
Contributor

jreback commented Jan 24, 2014

from_product 👍

@jreback
Copy link
Contributor

jreback commented Jan 24, 2014

@mwaskom FYI, use this PR number in release notes / v0.13.1 (as the reference issue)

@mwaskom
Copy link
Contributor Author

mwaskom commented Jan 25, 2014

OK, should be good

shades = ['light', 'dark']
colors = ['red', 'green', 'blue']

index = pandas.MultiIndex.from_product([shades, colors],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont't put the index = (if you assign to a variable then ipython wont' show it), and don't need the pandas everything is imported here

@jreback
Copy link
Contributor

jreback commented Jan 25, 2014

small changes

pls rebase and squash into 1 commit, see here: https://github.com/pydata/pandas/wiki/Using-Git

@mwaskom
Copy link
Contributor Author

mwaskom commented Jan 25, 2014

OK, sorry, git question.

I rebased and squashed, i.e. git log now shows

commit 493eb08fd59c4776b2a072b37646fef3aa09d661
Author: mwaskom <mwaskom@stanford.edu>
Date:   Thu Jan 23 18:57:22 2014 -0800

    ENH Add MultiIndex.from_product convenience function

    FIX typo in from_iterables docstring

    ENH Rename from_iterables to from_product

    TST rework from_product test to compare against from_tuples

    DOC Add notes on from_product to release notes and indexing docs

    DOC clean from_product examples in docs

commit b537df5f9d4a783f8edad20ce53968c74ad56985
Merge: 039c57d d6667b1
Author: jreback <jeff@reback.net>
Date:   Sat Jan 25 10:03:52 2014 -0800

    Merge pull request #6097 from jreback/hdf_fix

    TST: don't compare inferred index freq on some tests as it may not be preser

But now I cannot push to this branch due to

[pandas]{multiindex_from_iterables}$ git push origin multiindex_from_iterables
Username for 'https://github.com': mwaskom
Password for 'https://mwaskom@github.com':
To https://github.com/mwaskom/pandas.git
 ! [rejected]        multiindex_from_iterables -> multiindex_from_iterables (non-fast-forward)
error: failed to push some refs to 'https://github.com/mwaskom/pandas.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.

What's the right thing to do?

@jreback
Copy link
Contributor

jreback commented Jan 25, 2014

try this

git fetch upstream/master
git rebase -i upstream/master

remove all but your commit

then

git push origin thisbranchname -f

(the -f force pushes)

@mwaskom
Copy link
Contributor Author

mwaskom commented Jan 25, 2014

OK that's what I thought but wasn't sure.

@jreback
Copy link
Contributor

jreback commented Jan 25, 2014

@mwaskom excellent git-fu

any other comments

@jorisvandenbossche
@jtratner

shades = ['light', 'dark']
colors = ['red', 'green', 'blue']

MultiIndex.from_product([shades, colors], names=['shade', 'color'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These three code lines need an indentation. Also, I think they are meant as code that should be ran? Then the code-block -> ipython

FIX typo in from_iterables docstring

ENH Rename from_iterables to from_product

TST rework from_product test to compare against from_tuples

DOC Add notes on from_product to release notes and indexing docs

DOC clean from_product examples in docs

DOC Further clarify from_product docs
@mwaskom
Copy link
Contributor Author

mwaskom commented Jan 25, 2014

I think that addresses everything.

@jreback
Copy link
Contributor

jreback commented Jan 25, 2014

@jorisvandenbossche ok by me...go ahead and merge if you are ok

@jorisvandenbossche
Copy link
Member

Yes, certainly fine. Thanks a lot @mwaskom !

jorisvandenbossche added a commit that referenced this pull request Jan 25, 2014
ENH Add MultiIndex.from_product convenience function
@jorisvandenbossche jorisvandenbossche merged commit 63ce94d into pandas-dev:master Jan 25, 2014
@ghost
Copy link

ghost commented Jan 27, 2014

I just reached for this and it was there. 👍

@jreback jreback mentioned this pull request Feb 8, 2014
@mwaskom mwaskom deleted the multiindex_from_iterables branch September 4, 2021 21:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants