|
13 | 13 | from pandas.core.dtypes.common import is_period_dtype, needs_i8_conversion
|
14 | 14 |
|
15 | 15 | import pandas as pd
|
16 |
| -from pandas import CategoricalIndex, MultiIndex, RangeIndex |
| 16 | +from pandas import ( |
| 17 | + CategoricalIndex, |
| 18 | + DatetimeIndex, |
| 19 | + MultiIndex, |
| 20 | + PeriodIndex, |
| 21 | + RangeIndex, |
| 22 | + TimedeltaIndex, |
| 23 | +) |
17 | 24 | import pandas._testing as tm
|
18 | 25 |
|
19 | 26 |
|
@@ -391,3 +398,46 @@ def test_astype_preserves_name(self, index, dtype):
|
391 | 398 | assert result.names == index.names
|
392 | 399 | else:
|
393 | 400 | assert result.name == index.name
|
| 401 | + |
| 402 | + |
| 403 | +@pytest.mark.parametrize("na_position", [None, "middle"]) |
| 404 | +def test_sort_values_invalid_na_position(index_with_missing, na_position): |
| 405 | + if isinstance(index_with_missing, (DatetimeIndex, PeriodIndex, TimedeltaIndex)): |
| 406 | + # datetime-like indices will get na_position kwarg as part of |
| 407 | + # synchronizing duplicate-sorting behavior, because we currently expect |
| 408 | + # them, other indices, and Series to sort differently (xref 35922) |
| 409 | + pytest.xfail("sort_values does not support na_position kwarg") |
| 410 | + elif isinstance(index_with_missing, (CategoricalIndex, MultiIndex)): |
| 411 | + pytest.xfail("missing value sorting order not defined for index type") |
| 412 | + |
| 413 | + if na_position not in ["first", "last"]: |
| 414 | + with pytest.raises( |
| 415 | + ValueError, match=f"invalid na_position: {na_position}", |
| 416 | + ): |
| 417 | + index_with_missing.sort_values(na_position=na_position) |
| 418 | + |
| 419 | + |
| 420 | +@pytest.mark.parametrize("na_position", ["first", "last"]) |
| 421 | +def test_sort_values_with_missing(index_with_missing, na_position): |
| 422 | + # GH 35584. Test that sort_values works with missing values, |
| 423 | + # sort non-missing and place missing according to na_position |
| 424 | + |
| 425 | + if isinstance(index_with_missing, (DatetimeIndex, PeriodIndex, TimedeltaIndex)): |
| 426 | + # datetime-like indices will get na_position kwarg as part of |
| 427 | + # synchronizing duplicate-sorting behavior, because we currently expect |
| 428 | + # them, other indices, and Series to sort differently (xref 35922) |
| 429 | + pytest.xfail("sort_values does not support na_position kwarg") |
| 430 | + elif isinstance(index_with_missing, (CategoricalIndex, MultiIndex)): |
| 431 | + pytest.xfail("missing value sorting order not defined for index type") |
| 432 | + |
| 433 | + missing_count = np.sum(index_with_missing.isna()) |
| 434 | + not_na_vals = index_with_missing[index_with_missing.notna()].values |
| 435 | + sorted_values = np.sort(not_na_vals) |
| 436 | + if na_position == "first": |
| 437 | + sorted_values = np.concatenate([[None] * missing_count, sorted_values]) |
| 438 | + else: |
| 439 | + sorted_values = np.concatenate([sorted_values, [None] * missing_count]) |
| 440 | + expected = type(index_with_missing)(sorted_values) |
| 441 | + |
| 442 | + result = index_with_missing.sort_values(na_position=na_position) |
| 443 | + tm.assert_index_equal(result, expected) |
0 commit comments