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

Support for use of Enums in MultiIndex #21348

Closed
wants to merge 25 commits into from
Closed

Support for use of Enums in MultiIndex #21348

wants to merge 25 commits into from

Conversation

benediamond
Copy link

@benediamond benediamond commented Jun 7, 2018

  • closes Support for Enums in MultiIndex #21298
  • tests added / passed: added test_use_enum_in_multiindex in tests/indexes/test_multi.py, see diff
  • passes git diff upstream/master -u -- "*.py" | flake8 --diff
  • whatsnew entry

The boolean flag in

cat = Categorical(values, ordered=True)
categories = cat.categories
codes = cat.codes
incorrectly caused a TypeError to be raised in
raise TypeError("'values' is not ordered, please "
"explicitly specify the categories order "
"by passing in a categories argument.")

when an Enum was used in a MultiIndex. No tests fail when the flag is flipped back. This is fixed in this PR.

Due to an unnecessary "orderedness" check in `arrays/categorical.py`'s method `_factorize_from_iterable`, MultiIndex's constructor inappropriately threw a `TypeError` when passed an Enum as one of its iterables. This has been fixed.
@benediamond
Copy link
Author

benediamond commented Jun 7, 2018

CircleCI failed due to unavailability of module enum in test environment. I don't know how else to test this---should I simply remove the new test I added?

@gfyoung
Copy link
Member

gfyoung commented Jun 7, 2018

CircleCI failed due to unavailability of module enum in test environment. I don't know how else to test this---should I simply remove the new test I added?

@benediamond : I believe the build that's failing is a Python 2.7 build. I would use pytest's skip functionality to skip whenever we're in Python 2 land.

# cannot actually be built yet.
from pandas.core.algorithms import take_1d

def _get_ilevel_values(index, level):
Copy link
Contributor

Choose a reason for hiding this comment

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

what is all this for?

simply construct the expected index directly

Copy link
Author

Choose a reason for hiding this comment

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

The thought here is that the expected index cannot be constructed directly, due to the exact bug I'm trying to fix. Thus instead I create an expected "level", and verify that it matches the relevant level of df's index. This is done by replicating a sub-step of tm.assert_index_equal.

values = unique._shallow_copy(filled, name=index.names[level])
return values

from enum import Enum
Copy link
Contributor

Choose a reason for hiding this comment

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

all imports go at the top

return values

from enum import Enum
MyEnum = Enum("MyEnum", "A B")
Copy link
Contributor

Choose a reason for hiding this comment

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

you would have to add enum34 (the 2.7 backport) in our 2.7 builds

(pandas) bash-3.2$ grep python=2.7 ci/*.yaml
appveyor-27.yaml:  - python=2.7.*
circle-27-compat.yaml:  - python=2.7*
travis-27-locale.yaml:  - python=2.7
travis-27.yaml:  - python=2.7*

Copy link
Author

Choose a reason for hiding this comment

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

ok, I've hopefully done this correctly.

@codecov
Copy link

codecov bot commented Jun 7, 2018

Codecov Report

Merging #21348 into master will decrease coverage by 0.39%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master   #21348     +/-   ##
=========================================
- Coverage   92.28%   91.89%   -0.4%     
=========================================
  Files         161      153      -8     
  Lines       51500    49596   -1904     
=========================================
- Hits        47528    45576   -1952     
- Misses       3972     4020     +48
Flag Coverage Δ
#multiple 90.29% <100%> (-0.39%) ⬇️
#single 41.86% <100%> (-0.45%) ⬇️
Impacted Files Coverage Δ
pandas/core/arrays/categorical.py 95.69% <100%> (+0.34%) ⬆️
pandas/core/arrays/base.py 83.95% <0%> (-13.41%) ⬇️
pandas/util/_doctools.py 0% <0%> (-12.88%) ⬇️
pandas/io/parquet.py 71.79% <0%> (-12.83%) ⬇️
pandas/core/reshape/util.py 90.32% <0%> (-9.68%) ⬇️
pandas/util/_decorators.py 82.25% <0%> (-9.09%) ⬇️
pandas/io/formats/html.py 88.85% <0%> (-8.83%) ⬇️
pandas/core/dtypes/base.py 92.1% <0%> (-7.9%) ⬇️
pandas/errors/__init__.py 92.3% <0%> (-7.7%) ⬇️
pandas/core/common.py 92.09% <0%> (-6.32%) ⬇️
... and 144 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 20ae454...06437cd. Read the comment docs.

@benediamond
Copy link
Author

@gfyoung I'm stumped by this whatsnew conflict, any thoughts?

@@ -88,6 +88,7 @@ Indexing
- Bug in :meth:`MultiIndex.set_names` where error raised for a ``MultiIndex`` with ``nlevels == 1`` (:issue:`21149`)
- Bug in :class:`IntervalIndex` constructors where creating an ``IntervalIndex`` from categorical data was not fully supported (:issue:`21243`, issue:`21253`)
- Bug in :meth:`MultiIndex.sort_index` which was not guaranteed to sort correctly with ``level=1``; this was also causing data misalignment in particular :meth:`DataFrame.stack` operations (:issue:`20994`, :issue:`20945`, :issue:`21052`)
- Bug in :func:`pandas.core.arrays.categorical._factorize_from_iterable` inappropriately caused ``TypeError`` to be raised when an ``Enum`` was used as a factor in a ``MultiIndex`` (:issue:`21298`)
Copy link
Member

Choose a reason for hiding this comment

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

@gfyoung I'm stumped by this whatsnew conflict, any thoughts?

@benediamond : I would actually move your whatsnew addition to 0.24.0. That will suffice. 😄

Copy link
Author

@benediamond benediamond Jun 8, 2018

Choose a reason for hiding this comment

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

I've gone ahead and just removed it---I'm not sure where it belongs, as it appeared that this PR (when it was passing) was slated for the next milestone.

Copy link
Member

Choose a reason for hiding this comment

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

@benediamond : I would put it in 0.24.0, as I suggested above. We will need a whatsnew for this for sure, and that would be the best place to put it.

Copy link
Author

Choose a reason for hiding this comment

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

@gfyoung Done. this one didn't create merge conflicts, thankfully

labels, fill_value=unique._na_value
)
values = unique._shallow_copy(filled, name=index.names[level])
return values
Copy link
Member

@gfyoung gfyoung Jun 8, 2018

Choose a reason for hiding this comment

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

Given that you use this function only once, I think this abstraction is unnecessary as @jreback alluded to in an earlier comment.

Copy link
Author

Choose a reason for hiding this comment

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

Ok, amended. Thanks for your guidance.

# GH 21298
# Allow use of Enums as one of the factors in a MultiIndex.
from enum import Enum
from pandas.core.algorithms import take_1d
Copy link
Member

@gfyoung gfyoung Jun 8, 2018

Choose a reason for hiding this comment

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

By "top", @jreback meant move the imports to "top of the file" 😄

Copy link
Author

Choose a reason for hiding this comment

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

Hah whoops. done

This nested function had been drawn directly from _within_ `pands.util.testing.assert_index_equal`. As there is no way to construct the target dataframe's (multi) index directly (hence this PR), I simply compare this index's _0th level_ to a manually constructed (simple) index. This comparison is given by a subroutine of `assert_index_equal` (which applies the subroutine to _each_ level).
@@ -2523,7 +2523,7 @@ def _factorize_from_iterable(values):
ordered=values.ordered)
codes = values.codes
else:
cat = Categorical(values, ordered=True)
cat = Categorical(values, ordered=False)
Copy link
Member

Choose a reason for hiding this comment

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

Interesting that this didn't cause any testing issues...

Copy link
Author

@benediamond benediamond Jun 9, 2018

Choose a reason for hiding this comment

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

Yes, it's striking. It's possible that thatindexes.multi is essentially the only caller of categorical._factorize_from_iterable, and that the calls that multi makes happen to be such that within Categorical's constructor, control is always routed so that the only time that ordered is used is to incorrectly raise the TypeError on lines 352-354. That's my best-guess explanation, at least.

Copy link
Member

@gfyoung gfyoung left a comment

Choose a reason for hiding this comment

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

Well, this looks pretty good to me!

cc @jreback

@benediamond
Copy link
Author

@jreback bumping this.

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

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

pls add a whatsnew note for 0.24

@@ -22,6 +22,7 @@ dependencies:
# universal
- pytest
- pytest-xdist
- enum34
Copy link
Contributor

Choose a reason for hiding this comment

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

can you remove from all but 1 of the 27 compat tests (the travis-27) one

Copy link
Author

Choose a reason for hiding this comment

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

done

@@ -3307,3 +3308,28 @@ def test_duplicate_multiindex_labels(self):
with pytest.raises(ValueError):
ind.set_levels([['A', 'B', 'A', 'A', 'B'], [2, 1, 3, -2, 5]],
inplace=True)

def test_use_enum_in_multiindex(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

can you use @td.skip_if_no('enum') here (then do the actual import inside), we have to guard against the tests being run on 2.7 w/o the compat library (by a user).

Copy link
Author

Choose a reason for hiding this comment

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

filled = take_1d(unique.values, labels, fill_value=unique._na_value)
df_index_0 = unique._shallow_copy(filled, name=df.columns.names[0])
exp_index_0 = pd.Index([MyEnum.A, MyEnum.A, MyEnum.B, MyEnum.B],
dtype='object')
Copy link
Contributor

Choose a reason for hiding this comment

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

this is pretty complicated, why cannot you just directly construct the Index here?

Copy link
Author

Choose a reason for hiding this comment

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

@benediamond
Copy link
Author

can you use @td.skip_if_no('enum') here (then do the actual import inside), we have to guard against the tests being run on 2.7 w/o the compat library (by a user).

@jreback implemented here:

    @td.skip_if_no('enum')
    def test_use_enum_in_multiindex(self):
        # GH 21298
        # Allow use of Enums as one of the factors in a MultiIndex.
        from enum import Enum
        MyEnum = Enum("MyEnum", "A B")
        ...

but yields:

==================================== ERRORS ====================================
_____________ ERROR collecting pandas/tests/indexes/test_multi.py ______________
pandas/tests/indexes/test_multi.py:30: in <module>
    class TestMultiIndex(Base):
pandas/tests/indexes/test_multi.py:3310: in TestMultiIndex
    @td.skip_if_no('enum')
E   NameError: name 'td' is not defined
------- generated xml file: /tmp/circle-junit.2ZpyFLd/reports/junit.xml --------
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!
===================== 3 skipped, 1 error in 43.23 seconds ======================

not sure what's going on 😩

@TomAugspurger
Copy link
Contributor

I think you need to import pandas.util._test_decorators as td at the top of test_multi.py

@jreback
Copy link
Contributor

jreback commented Nov 21, 2018

closing as stale, if you'd like to continue, pls ping.

@jreback jreback closed this Nov 21, 2018
@benediamond
Copy link
Author

@jreback, my understanding was that this commit was ready, but had been waiting for your approval. I found it hard to get responses from you. if you're ready to go, i'm ready to play my part. it should be almost there.

@jreback
Copy link
Contributor

jreback commented Nov 21, 2018

i left multiple comments
you can update and rebase in master

@jreback jreback reopened this Nov 21, 2018
@pep8speaks
Copy link

Hello @benediamond! Thanks for updating the PR.

@@ -132,7 +132,7 @@ Strings
Indexing
^^^^^^^^

-
- Bug in :func:`pandas.core.arrays.categorical._factorize_from_iterable` inappropriately caused ``TypeError`` to be raised when an ``Enum`` was used as a factor in a ``MultiIndex`` (:issue:`21298`)
Copy link
Contributor

Choose a reason for hiding this comment

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

Release notes shouldn't reference private methods.

You should be able to reference Enum with :class:enum.Enum, and :class:MultiIndex.

@benediamond
Copy link
Author

benediamond commented Nov 22, 2018

@jreback and others: looks like this issue has since been fixed by PR #22072, see also issue #15457. I have gone ahead and preserved the test I wrote in my rebase, in case you'd like to merge it in---if not no worries and this (as well as issue #21298) can be closed! (note that issue #15457 was a somewhat different problem). thanks for everyone's time and patience.

@WillAyd
Copy link
Member

WillAyd commented Nov 26, 2018

@benediamond closing per your latest comment. Ping if we need to reopen

@WillAyd WillAyd closed this Nov 26, 2018
@benediamond benediamond deleted the bd-21298 branch November 26, 2018 18:15
@scratchmex
Copy link

scratchmex commented May 12, 2022

I want to point out that when merging dataframes with enums as multi-index it throws
RuntimeWarning: The values in the array are unorderable. Pass `sort=False` to suppress this warning.
so I think the support for enums is not yet completed

@jreback
Copy link
Contributor

jreback commented May 12, 2022

you could open a separate issue with a reproducible example

but we have no official support for enums and would need a champion in the community

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

Successfully merging this pull request may close these issues.

Support for Enums in MultiIndex
7 participants