-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Always cast to Categorical in lexsort_indexer #36385
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
BUG: Always cast to Categorical in lexsort_indexer #36385
Conversation
Not sure if this is related, but I noticed a problem in Categorical.sort_values setting self._codes = foo instead of self._codes[:] = foo |
categories = ["c", "b", "a"] | ||
df = pd.DataFrame({"x": [1, 1, 1], "y": ["a", "b", "c"]}) | ||
|
||
def sorter(key): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you try when the categorical is ordered=False as well (parameterize)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah this is broken actually. Will need to be more careful above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or maybe my expectation is off about how this should behave when the categorical is unordered. This is odd (it seems to respect the order in which categories are given even when ordered=False):
[ins] In [1]: import pandas as pd
...:
...:
...: values = ["a", "b", "c"]
...:
...: cat = pd.Categorical(values, categories=["a", "b", "c"], ordered=False)
...: print(cat.sort_values())
...:
...: cat = pd.Categorical(values, categories=["c", "b", "a"], ordered=False)
...: print(cat.sort_values())
...:
...: print(pd.__version__)
...:
['a', 'b', 'c']
Categories (3, object): ['a', 'b', 'c']
['c', 'b', 'a']
Categories (3, object): ['c', 'b', 'a']
1.2.0.dev0+390.g595791b6f.dirty
Maybe sorting an unordered categorical should actually be raising.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe sorting an unordered categorical should actually be raising.
had this discussion with @jorisvandenbossche a while back......
yeah sorting just gives back the same ordering as the categories that you have, they just don't mean anything.
so we do allow it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what I think is broken is actually this
In [184]: pd.Categorical(pd.Categorical(values, categories=["a", "b", "c"], ordered=False), ordered=True)
Out[184]:
[a, b, c]
Categories (3, object): [a < b < c]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should raise, though we do allow this via .set_ordered()
so maybe its ok
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since we aren't actually testing this likely i think prob ok to merge this and open an issue for discussion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would almost expect sorting an unordered categorical to simply return the original array (since maybe you could argue it's already "trivially ordered" in some sense) if it weren't to raise
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, R does the same thing as pandas interestingly enough:
> x <- factor(c("a", "b", "c", "a"), levels = c("c", "b", "a"), ordered = FALSE)
> x
[1] a b c a
Levels: c b a
> sort(x)
[1] c b a a
Levels: c b a
I guess because it's easiest just to always sort by the underlying codes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would almost expect sorting an unordered categorical to simply return the original array (since maybe you could argue it's already "trivially ordered" in some sense)
@dsaxton strings also don't necessarily have a meaningfull order, but we still sort them lexicographically. In the same way, we still sort an unordered categorical, using the order of the categories (which is the same as lexicographically sorted in most cases, unless you specified the categories manually in a certain order).
There are lots of reasons to allow sorting for an "unordered" categorical. One example is to get a deterministic order of your values, which can be useful regardless of the order of the categories having a meaning or not.
thanks @dsaxton |
@jreback you milestone 1.2. change note in 1.1.3. which should it be? |
@jreback ok to backport? see #36385 (comment) |
yeah this is ok |
thanks |
@meeseeksdev backport 1.1.x |
#36477) Co-authored-by: Daniel Saxton <[email protected]>
black pandas
git diff upstream/master -u -- "*.py" | flake8 --diff