-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
REF/BUG/API: factorizing categorical data #19938
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
Merged
jreback
merged 14 commits into
pandas-dev:master
from
TomAugspurger:categorical-factorize
Mar 15, 2018
Merged
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
18de376
REF/BUG/API: factorizing categorical data
TomAugspurger 9ef5be2
Explicit dtype for expected
TomAugspurger 5e52b6f
Clean : imports / remove sort
TomAugspurger e19ae86
Merge remote-tracking branch 'upstream/master' into categorical-facto…
TomAugspurger 121b682
Restore sort
TomAugspurger a6bc405
REF: remove sort from Categorical.factorize
TomAugspurger 0bfbc47
Updated comment
TomAugspurger b25f383
Merge remote-tracking branch 'upstream/master' into categorical-facto…
TomAugspurger 2688c4f
Fixed new sort algo
TomAugspurger 2395a99
Merge remote-tracking branch 'upstream/master' into categorical-facto…
TomAugspurger ab4f01c
Implement interface
TomAugspurger 65150f4
Merge remote-tracking branch 'upstream/master' into categorical-facto…
TomAugspurger ad8173b
Merge remote-tracking branch 'upstream/master' into categorical-facto…
TomAugspurger 1e006d1
Merge remote-tracking branch 'upstream/master' into categorical-facto…
TomAugspurger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Restore sort
- Loading branch information
commit 121b682ab4c7b912df2a814305560b23467154f4
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,8 +30,7 @@ | |
is_scalar, | ||
is_dict_like) | ||
|
||
from pandas.core.algorithms import ( | ||
factorize, take_1d, unique1d, _factorize_array) | ||
from pandas.core.algorithms import factorize, take_1d, unique1d | ||
from pandas.core.accessor import PandasDelegate | ||
from pandas.core.base import (PandasObject, | ||
NoNewAttributesMixin, _shared_docs) | ||
|
@@ -366,10 +365,6 @@ def __init__(self, values, categories=None, ordered=None, dtype=None, | |
self._dtype = self._dtype.update_dtype(dtype) | ||
self._codes = coerce_indexer_dtype(codes, dtype.categories) | ||
|
||
@classmethod | ||
def _constructor_from_sequence(cls, scalars): | ||
return cls(scalars) | ||
|
||
@property | ||
def categories(self): | ||
"""The categories of this categorical. | ||
|
@@ -2074,11 +2069,13 @@ def unique(self): | |
take_codes = sorted(take_codes) | ||
return cat.set_categories(cat.categories.take(take_codes)) | ||
|
||
def factorize(self, na_sentinel=-1): | ||
def factorize(self, sort=False, na_sentinel=-1): | ||
"""Encode the Categorical as an enumerated type. | ||
|
||
Parameters | ||
---------- | ||
sort : boolean, default False | ||
Sort by values | ||
na_sentinel: int, default -1 | ||
Value to mark "not found" | ||
|
||
|
@@ -2113,6 +2110,7 @@ def factorize(self, na_sentinel=-1): | |
[a, b] | ||
Categories (2, object): [a, b] | ||
""" | ||
from pandas.core.algorithms import _factorize_array, take_1d | ||
|
||
codes = self.codes.astype('int64') | ||
codes[codes == -1] = iNaT | ||
|
@@ -2123,6 +2121,10 @@ def factorize(self, na_sentinel=-1): | |
uniques = self._constructor(self.categories.take(uniques), | ||
categories=self.categories, | ||
ordered=self.ordered) | ||
if sort: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would remove the sort from here and just do it in factorize part (after the else), otherwise logic is in multiple places here |
||
order = uniques.argsort() | ||
labels = take_1d(order, labels, fill_value=na_sentinel) | ||
uniques = uniques.take(order) | ||
return labels, uniques | ||
|
||
def equals(self, other): | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The interface we have to
hashtable.get_labels()
is very odd right now, IOW we have acheck_null
flag which then makes the caller know to substitute values to iNaT (for int64) and know which are the sentinels. This is breaking the abstrastion. Rather would either like to be able to pass in the actual sentinel (not the output sentinel, but that's another confusion). e.g . you would simply pass -1 here.I think its worth re-factoring this (maybe before this PR), though I suppose could be after.
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.
#20328
Yes, that'd be nicer.
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.
do we actually want this to be public?
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.
factorize in general? I don’t see why not. It’s present on series and index.
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.
#19938 (comment) was in reference to the API docs. We whitelist the methods on Categorical that are included in the API docs (just
__array__
andfrom_codes
for now).