|
14 | 14 | from pandas.util.testing import assert_series_equal
|
15 | 15 | import pandas.util.testing as tm
|
16 | 16 | from pandas.tests.series.common import TestData
|
| 17 | +from pandas._libs.tslib import iNaT |
| 18 | +from pandas._libs.algos import Infinity, NegInfinity |
17 | 19 |
|
18 | 20 |
|
19 | 21 | class TestSeriesRank(TestData):
|
@@ -196,14 +198,40 @@ def test_rank_signature(self):
|
196 | 198 | pytest.raises(ValueError, s.rank, 'average')
|
197 | 199 |
|
198 | 200 | def test_rank_inf(self):
|
199 |
| - values = np.array( |
| 201 | + # test ranking -/+ inf together with randomly inserted na values |
| 202 | + mock_contents = ( |
200 | 203 | [-np.inf, -50, -1, -1e-20, -1e-25, -1e-50, 0, 1e-40, 1e-20, 1e-10,
|
201 |
| - 2, 40, np.inf], dtype='float64') |
202 |
| - random_order = np.random.permutation(len(values)) |
203 |
| - iseries = Series(values[random_order]) |
204 |
| - exp = Series(random_order + 1.0, dtype='float64') |
205 |
| - iranks = iseries.rank() |
206 |
| - assert_series_equal(iranks, exp) |
| 204 | + 2, 40, np.inf], |
| 205 | + [-np.inf, -50, -1, -1e-20, -1e-25, -1e-45, 0, 1e-40, 1e-20, 1e-10, |
| 206 | + 2, 40, np.inf], |
| 207 | + [np.iinfo(np.uint8).min, 1, 2, 100, np.iinfo(np.uint8).max], |
| 208 | + # -inf is eqivatlent to na value for int64 dtype becasue iNaT is |
| 209 | + # the same as np.iinfo(np.int64).min. The expected rank for iNaT is |
| 210 | + # nan if keep_na is true. Pending GH issue #16674 |
| 211 | + [np.iinfo(np.int64).min + 1, -100, 0, 1, 9999, 100000, |
| 212 | + 1e10, np.iinfo(np.int64).max], |
| 213 | + [NegInfinity(), '1', 'A', 'BA', 'Ba', 'C', Infinity()] |
| 214 | + ) |
| 215 | + dtypes = ('float64', 'float32', 'uint8', 'int64', 'object') |
| 216 | + dtype_na_map = { |
| 217 | + 'float64': np.nan, |
| 218 | + 'float32': np.nan, |
| 219 | + 'int64': iNaT, |
| 220 | + 'object': None |
| 221 | + } |
| 222 | + for content, dtype in zip(mock_contents, dtypes): |
| 223 | + values = np.array(content, dtype=dtype) |
| 224 | + nan_indices = np.random.choice(range(len(values)), 3) |
| 225 | + exp_order = np.array(range(len(values)), dtype='float64') + 1.0 |
| 226 | + if dtype in dtype_na_map: |
| 227 | + na_value = dtype_na_map[dtype] |
| 228 | + values = np.insert(values, nan_indices, na_value) |
| 229 | + exp_order = np.insert(exp_order, nan_indices, np.nan) |
| 230 | + random_order = np.random.permutation(len(values)) |
| 231 | + iseries = Series(values[random_order]) |
| 232 | + exp = Series(exp_order[random_order], dtype='float64') |
| 233 | + iranks = iseries.rank() |
| 234 | + assert_series_equal(iranks, exp) |
207 | 235 |
|
208 | 236 | def test_rank_tie_methods(self):
|
209 | 237 | s = self.s
|
|
0 commit comments