|
7 | 7 | from numpy import nan |
8 | 8 | import numpy as np |
9 | 9 |
|
10 | | -from pandas import (Series, date_range, NaT) |
| 10 | +from pandas import Series, date_range, NaT |
| 11 | +from pandas.api.types import CategoricalDtype |
11 | 12 |
|
12 | 13 | from pandas.compat import product |
13 | 14 | from pandas.util.testing import assert_series_equal |
@@ -123,50 +124,34 @@ def test_rank_categorical(self): |
123 | 124 | exp_desc = Series([6., 5., 4., 3., 2., 1.]) |
124 | 125 | ordered = Series( |
125 | 126 | ['first', 'second', 'third', 'fourth', 'fifth', 'sixth'] |
126 | | - ).astype( |
127 | | - 'category', |
128 | | - categories=['first', 'second', 'third', |
129 | | - 'fourth', 'fifth', 'sixth'], |
130 | | - ordered=True |
131 | | - ) |
| 127 | + ).astype(CategoricalDtype(categories=['first', 'second', 'third', |
| 128 | + 'fourth', 'fifth', 'sixth'], |
| 129 | + ordered=True)) |
132 | 130 | assert_series_equal(ordered.rank(), exp) |
133 | 131 | assert_series_equal(ordered.rank(ascending=False), exp_desc) |
134 | 132 |
|
135 | 133 | # Unordered categoricals should be ranked as objects |
136 | | - unordered = Series( |
137 | | - ['first', 'second', 'third', 'fourth', 'fifth', 'sixth'], |
138 | | - ).astype( |
139 | | - 'category', |
140 | | - categories=['first', 'second', 'third', |
141 | | - 'fourth', 'fifth', 'sixth'], |
142 | | - ordered=False |
143 | | - ) |
| 134 | + unordered = Series(['first', 'second', 'third', 'fourth', |
| 135 | + 'fifth', 'sixth']).astype( |
| 136 | + CategoricalDtype(categories=['first', 'second', 'third', |
| 137 | + 'fourth', 'fifth', 'sixth'], |
| 138 | + ordered=False)) |
144 | 139 | exp_unordered = Series([2., 4., 6., 3., 1., 5.]) |
145 | 140 | res = unordered.rank() |
146 | 141 | assert_series_equal(res, exp_unordered) |
147 | 142 |
|
148 | 143 | unordered1 = Series( |
149 | 144 | [1, 2, 3, 4, 5, 6], |
150 | | - ).astype( |
151 | | - 'category', |
152 | | - categories=[1, 2, 3, 4, 5, 6], |
153 | | - ordered=False |
154 | | - ) |
| 145 | + ).astype(CategoricalDtype([1, 2, 3, 4, 5, 6], False)) |
155 | 146 | exp_unordered1 = Series([1., 2., 3., 4., 5., 6.]) |
156 | 147 | res1 = unordered1.rank() |
157 | 148 | assert_series_equal(res1, exp_unordered1) |
158 | 149 |
|
159 | 150 | # Test na_option for rank data |
160 | 151 | na_ser = Series( |
161 | 152 | ['first', 'second', 'third', 'fourth', 'fifth', 'sixth', np.NaN] |
162 | | - ).astype( |
163 | | - 'category', |
164 | | - categories=[ |
165 | | - 'first', 'second', 'third', 'fourth', |
166 | | - 'fifth', 'sixth', 'seventh' |
167 | | - ], |
168 | | - ordered=True |
169 | | - ) |
| 153 | + ).astype(CategoricalDtype(['first', 'second', 'third', 'fourth', |
| 154 | + 'fifth', 'sixth', 'seventh'], True)) |
170 | 155 |
|
171 | 156 | exp_top = Series([2., 3., 4., 5., 6., 7., 1.]) |
172 | 157 | exp_bot = Series([1., 2., 3., 4., 5., 6., 7.]) |
@@ -195,13 +180,8 @@ def test_rank_categorical(self): |
195 | 180 | ) |
196 | 181 |
|
197 | 182 | # Test with pct=True |
198 | | - na_ser = Series( |
199 | | - ['first', 'second', 'third', 'fourth', np.NaN], |
200 | | - ).astype( |
201 | | - 'category', |
202 | | - categories=['first', 'second', 'third', 'fourth'], |
203 | | - ordered=True |
204 | | - ) |
| 183 | + na_ser = Series(['first', 'second', 'third', 'fourth', np.NaN]).astype( |
| 184 | + CategoricalDtype(['first', 'second', 'third', 'fourth'], True)) |
205 | 185 | exp_top = Series([0.4, 0.6, 0.8, 1., 0.2]) |
206 | 186 | exp_bot = Series([0.2, 0.4, 0.6, 0.8, 1.]) |
207 | 187 | exp_keep = Series([0.25, 0.5, 0.75, 1., np.NaN]) |
|
0 commit comments