Skip to content

Commit 049c0fc

Browse files
committed
GH#15420 added support for na_option when ranking categorical
1 parent 5e5bbeb commit 049c0fc

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

doc/source/whatsnew/v0.20.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -628,3 +628,4 @@ Bug Fixes
628628
- Bug in ``Series.replace`` and ``DataFrame.replace`` which failed on empty replacement dicts (:issue:`15289`)
629629
- Bug in ``pd.melt()`` where passing a tuple value for ``value_vars`` caused a ``TypeError`` (:issue:`15348`)
630630
- Bug in ``.eval()`` which caused multiline evals to fail with local variables not on the first line (:issue:`15342`)
631+
- Bug in ``.rank()`` rank incorrectly orders ordered categories

pandas/tests/series/test_analytics.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -1059,13 +1059,14 @@ def test_rank(self):
10591059

10601060
def test_rank_categorical(self):
10611061
# GH issue #15420 rank incorrectly orders ordered categories
1062-
1062+
10631063
# Test ascending/descending ranking for ordered categoricals
10641064
exp = pd.Series([1., 2., 3., 4., 5., 6.])
10651065
exp_desc = pd.Series([6., 5., 4., 3., 2., 1.])
1066-
ordered = pd.Series(
1067-
['first', 'second', 'third', 'fourth', 'fifth', 'sixth'],
1068-
).astype('category').cat.set_categories(
1066+
ser = pd.Series(
1067+
['first', 'second', 'third', 'fourth', 'fifth', 'sixth']
1068+
)
1069+
ordered = ser.astype('category', ).cat.set_categories(
10691070
['first', 'second', 'third', 'fourth', 'fifth', 'sixth'],
10701071
ordered=True
10711072
)
@@ -1088,6 +1089,15 @@ def test_rank_categorical(self):
10881089
['first', 'second', 'third', 'fourth', 'fifth', 'sixth', np.NaN],
10891090
).astype('category').cat.set_categories(
10901091
['first', 'second', 'third', 'fourth', 'fifth', 'sixth'],
1092+
1093+
# Test na_option for rank data
1094+
na_ser = pd.Series(
1095+
['first', 'second', 'third', 'fourth', 'fifth', 'sixth', np.NaN]
1096+
).astype('category', ).cat.set_categories(
1097+
[
1098+
'first', 'second', 'third', 'fourth',
1099+
'fifth', 'sixth', 'seventh'
1100+
],
10911101
ordered=True
10921102
)
10931103

pandas/tests/test_categorical.py

-8
Original file line numberDiff line numberDiff line change
@@ -4549,14 +4549,6 @@ def test_concat_categorical(self):
45494549
'h': [None] * 6 + cat_values})
45504550
tm.assert_frame_equal(res, exp)
45514551

4552-
def test_rank_categorical(self):
4553-
exp = pd.Series([1., 2., 3., 4., 5., 6.], name='A')
4554-
dframe = pd.DataFrame(['first', 'second', 'third', 'fourth', 'fifth', 'sixth'], columns=['A'])
4555-
dframe['A'] = dframe['A'].astype('category', ).cat.set_categories(
4556-
['first', 'second', 'third', 'fourth', 'fifth', 'sixth'], ordered=True)
4557-
res = dframe['A'].rank()
4558-
tm.assert_series_equal(res, exp)
4559-
45604552
class TestCategoricalSubclassing(tm.TestCase):
45614553

45624554
def test_constructor(self):

0 commit comments

Comments
 (0)