Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
BUG, DEP, DOC: Patch and Align Categorical's Sorting API #12882
Conversation
jreback
added Categorical Docs
labels
Apr 12, 2016
jreback
and 1 other
commented on an outdated diff
Apr 12, 2016
| + >>> c.sort_values() | ||
| + [1, 1, 2, 2, 5] | ||
| + Categories (3, int64): [1, 2, 5] | ||
| + >>> c.sort_values(ascending=False) | ||
| + [5, 2, 2, 1, 1] | ||
| + Categories (3, int64): [1, 2, 5] | ||
| + | ||
| + Inplace sorting can be done as well: | ||
| + | ||
| + >>> c.sort_values(inplace=True) | ||
| + >>> c | ||
| + [1, 1, 2, 2, 5] | ||
| + Categories (3, int64): [1, 2, 5] | ||
| + >>> | ||
| + >>> c = pd.Categorical([1, 2, 2, 1, 5]) | ||
| + >>> c.sort() # equivalent call |
jreback
Contributor
|
jreback
and 1 other
commented on an outdated diff
Apr 12, 2016
jreback
and 1 other
commented on an outdated diff
Apr 12, 2016
| @@ -1307,6 +1307,57 @@ def test_sort(self): | ||
| exp = np.array(["a", "b", "c", "d"], dtype=object) | ||
| self.assert_numpy_array_equal(cat1.__array__(), exp) | ||
| + # reverse | ||
| + cat = Categorical(["a", "c", "c", "b", "d"], ordered=True) | ||
| + res = cat.sort_values(ascending=False) | ||
| + exp_val = np.array(["d", "c", "c", "b", "a"], dtype=object) | ||
| + exp_categories = np.array(["a", "b", "c", "d"], dtype=object) | ||
| + self.assert_numpy_array_equal(res.__array__(), exp_val) | ||
| + self.assert_numpy_array_equal(res.categories, exp_categories) | ||
| + | ||
| + # some NaN positions |
|
|
|
yeah I am pretty sure that so @gfyoung pls change the doc-string, have it call |
|
@jreback : Wait...so the wrong method was deprecated then? Because in the documentation, |
|
no |
|
Read your comment above |
|
all |
|
Okay, so everyone calls |
gfyoung
changed the title from
BUG, DOC: Clarify and patch Categorical's sort API to BUG, DEP, DOC: Clarify and patch Categorical's sort API
Apr 12, 2016
gfyoung
changed the title from
BUG, DEP, DOC: Clarify and patch Categorical's sort API to BUG, DEP, DOC: Patch and Align Categorical's Sorting API
Apr 12, 2016
jreback
and 1 other
commented on an outdated diff
Apr 12, 2016
| with tm.assert_produces_warning(FutureWarning): | ||
| - c.order() | ||
| + c.order() # deprecated in gh-10726 | ||
| + c.sort() # deprecated in gh-12882 |
jreback
Contributor
|
jreback
commented on the diff
Apr 12, 2016
| - Categorical.sort is the equivalent but sorts the Categorical inplace. | ||
| - | ||
| - Parameters | ||
| - ---------- | ||
| - inplace : boolean, default False | ||
| - Do operation in place. | ||
| - ascending : boolean, default True | ||
| - Sort ascending. Passing False sorts descending | ||
| - na_position : {'first', 'last'} (optional, default='last') | ||
| - 'first' puts NaNs at the beginning | ||
| - 'last' puts NaNs at the end | ||
| - | ||
| - Returns | ||
| - ------- | ||
| - y : Category or None | ||
| + DEPRECATED: use :meth:`Categorical.sort_values`. That function |
|
|
jsexauer
referenced
this pull request
Apr 12, 2016
Open
DEPR: deprecations from prior versions #6581
|
Is it possible there was a reason why |
jorisvandenbossche
commented on an outdated diff
Apr 12, 2016
| @@ -1157,30 +1157,74 @@ def argsort(self, ascending=True, **kwargs): | ||
| return result | ||
| def sort_values(self, inplace=False, ascending=True, na_position='last'): | ||
| - """ Sorts the Category by category value returning a new Categorical by | ||
| - default. | ||
| - | ||
| - Only ordered Categoricals can be sorted! | ||
| - | ||
| - Categorical.sort is the equivalent but sorts the Categorical inplace. | ||
| + """ Sorts the Categorical by category value returning a new | ||
| + Categorical by default. While an ordering is applied to the |
jorisvandenbossche
Owner
|
jorisvandenbossche
commented on the diff
Apr 12, 2016
| Parameters | ||
| ---------- | ||
| inplace : boolean, default False | ||
| Do operation in place. | ||
| ascending : boolean, default True | ||
| - Sort ascending. Passing False sorts descending | ||
| + Order ascending. Passing False orders descending. The |
jorisvandenbossche
Owner
|
|
@jorisvandenbossche no it was ADDED in that PR for conformity. with the Series/Index API. I think it was for conforming with a numpy-like object (which DOES have sort). But I don't think it really makes sense. |
jorisvandenbossche
commented on the diff
Apr 12, 2016
| Parameters | ||
| ---------- | ||
| inplace : boolean, default False | ||
| Do operation in place. | ||
| ascending : boolean, default True | ||
| - Sort ascending. Passing False sorts descending | ||
| + Order ascending. Passing False orders descending. The | ||
| + ordering parameter provides the method by which the | ||
| + category values are organized. |
gfyoung
Member
|
|
@jreback : Agreed. Judging from what I have seen with |
|
Looking back at PR #10726 (the sort unification PR), my conclusion is that |
|
|
@jorisvandenbossche hmm, ok maybe I am wrong about when this was added. But we have removed (well deprecated) |
|
|
|
@jreback : Travis is giving a green light once more, and I have addressed all of the changes / comments so far. Ready to merge if everyone is happy. |
jreback
added this to the
0.18.1
milestone
Apr 14, 2016
jreback
closed this
in 2ea0601
Apr 14, 2016
|
thanks! |
gfyoung
deleted the
gfyoung:categorical-sort-doc branch
Apr 14, 2016
gfyoung
added a commit
to gfyoung/pandas
that referenced
this pull request
Jun 19, 2017
|
|
gfyoung |
ab3264e
|
gfyoung
added a commit
to gfyoung/pandas
that referenced
this pull request
Jun 19, 2017
|
|
gfyoung |
9891a5b
|
This was referenced Jun 19, 2017
jreback
added a commit
that referenced
this pull request
Jun 19, 2017
|
|
gfyoung + jreback |
8b5e3d6
|
guillemborrell
added a commit
to guillemborrell/pandas
that referenced
this pull request
Jul 7, 2017
|
|
gfyoung + guillemborrell |
a69eb57
|
gfyoung commentedApr 12, 2016
Clarifies the meaning of 'sort' in the context of
Categoricalto mean 'organization' rather than 'order',as it is possible to call this method (as well as
sort_values) when theCategoricalis unordered.Also patches a bug in
Categorical.sort_valuesin whichna_positionwas not being respected whenascendingwas set toTrue. This commit aligns the behaviour with that ofSeries.Finally, deprecates
sortin favor ofsort_values, which is in alignment with what was done withSeriesback in #10726.Closes #12785.