Skip to content

Commit 8b81e55

Browse files
committed
Revert "remove new tests for debug"
This reverts commit 29d6849.
1 parent 29d6849 commit 8b81e55

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

pandas/conftest.py

+22
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,28 @@ def index(request):
437437
index_fixture2 = index
438438

439439

440+
@pytest.fixture(params=indices_dict.keys())
441+
def index_with_missing(request):
442+
"""
443+
Fixture for indices with missing values
444+
"""
445+
if request.param in ["int", "uint", "range", "empty", "repeats"]:
446+
pytest.xfail("missing values not supported")
447+
ind = indices_dict[request.param].copy()
448+
vals = ind.values
449+
if type(vals[0]) == tuple:
450+
# For setting missing values in the top level of MultiIndex
451+
vals = ind.tolist()
452+
vals[0] = tuple([None]) + vals[0][1:]
453+
vals[-1] = tuple([None]) + vals[-1][1:]
454+
ind = MultiIndex.from_tuples(vals)
455+
else:
456+
vals[0] = None
457+
vals[-1] = None
458+
ind = type(ind)(vals)
459+
return ind
460+
461+
440462
# ----------------------------------------------------------------
441463
# Series'
442464
# ----------------------------------------------------------------

pandas/tests/indexes/test_common.py

+37
Original file line numberDiff line numberDiff line change
@@ -398,3 +398,40 @@ def test_astype_preserves_name(self, index, dtype):
398398
assert result.names == index.names
399399
else:
400400
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+
406+
if type(index_with_missing) in [DatetimeIndex, PeriodIndex, TimedeltaIndex]:
407+
pytest.xfail("stable descending order sort not implemented")
408+
elif type(index_with_missing) in [CategoricalIndex, MultiIndex]:
409+
pytest.xfail("missing value sorting order not defined for index type")
410+
411+
if na_position not in ["first", "last"]:
412+
with pytest.raises(
413+
ValueError, match=f"invalid na_position: {na_position}",
414+
):
415+
index_with_missing.sort_values(na_position=na_position)
416+
417+
418+
@pytest.mark.parametrize("na_position", ["first", "last"])
419+
def test_sort_values_with_missing(index_with_missing, na_position):
420+
# GH 35584. Test that sort_values works with missing values,
421+
# sort non-missing and place missing according to na_position
422+
423+
if type(index_with_missing) in [DatetimeIndex, PeriodIndex, TimedeltaIndex]:
424+
pytest.xfail("stable descending order sort not implemented")
425+
elif type(index_with_missing) in [CategoricalIndex, MultiIndex]:
426+
pytest.xfail("missing value sorting order not defined for index type")
427+
missing_count = np.sum(index_with_missing.isna())
428+
not_na_vals = index_with_missing[index_with_missing.notna()].values
429+
sorted_values = np.sort(not_na_vals)
430+
if na_position == "first":
431+
sorted_values = np.concatenate([[None] * missing_count, sorted_values])
432+
else:
433+
sorted_values = np.concatenate([sorted_values, [None] * missing_count])
434+
expected = type(index_with_missing)(sorted_values)
435+
436+
result = index_with_missing.sort_values(na_position=na_position)
437+
tm.assert_index_equal(result, expected)

0 commit comments

Comments
 (0)