-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: allow missing values in Index when calling Index.sort_values #35604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 62 commits
076061b
3fa9351
333c6e4
8414fd0
fa12898
e2575c9
4293e22
275067e
098fe10
f0d4c2d
743eca6
9d17d7a
744e5c1
13fcd94
33b02da
73cc94c
153fb06
3481b81
9d92ddd
7ec9e7e
2d16b8d
8939040
066f0af
7e351cd
79ed8ee
62268e8
5cab3d4
30ac949
0ac0a3c
051c3e7
dfdee40
acf6170
4b43852
fb8f703
fc58c3f
1dfd4f2
65c001a
dd82cc1
a3855ad
481de59
c7e8240
ad29fff
6ae6e8a
213b13b
760bda1
d8f9061
2f8bec6
29d6849
8b81e55
5b2a0d3
a3310ba
e80bc9d
2007699
8fd5a77
2e39294
d78a9a2
5715c31
bfd2e9c
54a6e82
f60d2a8
af92fe8
6d33657
4935309
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,14 @@ | |
from pandas.core.dtypes.common import is_period_dtype, needs_i8_conversion | ||
|
||
import pandas as pd | ||
from pandas import CategoricalIndex, MultiIndex, RangeIndex | ||
from pandas import ( | ||
CategoricalIndex, | ||
DatetimeIndex, | ||
MultiIndex, | ||
PeriodIndex, | ||
RangeIndex, | ||
TimedeltaIndex, | ||
) | ||
import pandas._testing as tm | ||
|
||
|
||
|
@@ -391,3 +398,45 @@ def test_astype_preserves_name(self, index, dtype): | |
assert result.names == index.names | ||
else: | ||
assert result.name == index.name | ||
|
||
|
||
@pytest.mark.parametrize("na_position", [None, "middle"]) | ||
def test_sort_values_invalid_na_position(index_with_missing, na_position): | ||
if type(index_with_missing) in [DatetimeIndex, PeriodIndex, TimedeltaIndex]: | ||
# datetime-like indices will get na_position kwarg as part of | ||
# synchronizing duplicate-sorting behavior, because we currently expect | ||
# them, other indices, and Series to sort differently (xref 35922) | ||
pytest.xfail("sort_values does not support na_position kwarg") | ||
elif type(index_with_missing) in [CategoricalIndex, MultiIndex]: | ||
pytest.xfail("missing value sorting order not defined for index type") | ||
|
||
if na_position not in ["first", "last"]: | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
with pytest.raises( | ||
ValueError, match=f"invalid na_position: {na_position}", | ||
): | ||
index_with_missing.sort_values(na_position=na_position) | ||
|
||
|
||
@pytest.mark.parametrize("na_position", ["first", "last"]) | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def test_sort_values_with_missing(index_with_missing, na_position): | ||
# GH 35584. Test that sort_values works with missing values, | ||
# sort non-missing and place missing according to na_position | ||
|
||
if type(index_with_missing) in [DatetimeIndex, PeriodIndex, TimedeltaIndex]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jreback The tests in I've clarified the xfail messages everywhere and added comments with xrefs to the tests in Sorry if I misunderstood your question. |
||
# datetime-like indices will get na_position kwarg as part of | ||
# synchronizing duplicate-sorting behavior, because we currently expect | ||
# them, other indices, and Series to sort differently (xref 35922) | ||
pytest.xfail("sort_values does not support na_position kwarg") | ||
elif type(index_with_missing) in [CategoricalIndex, MultiIndex]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you isinstance(....) instead There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
pytest.xfail("missing value sorting order not defined for index type") | ||
missing_count = np.sum(index_with_missing.isna()) | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
not_na_vals = index_with_missing[index_with_missing.notna()].values | ||
sorted_values = np.sort(not_na_vals) | ||
if na_position == "first": | ||
sorted_values = np.concatenate([[None] * missing_count, sorted_values]) | ||
else: | ||
sorted_values = np.concatenate([sorted_values, [None] * missing_count]) | ||
expected = type(index_with_missing)(sorted_values) | ||
|
||
result = index_with_missing.sort_values(na_position=na_position) | ||
tm.assert_index_equal(result, expected) |
Uh oh!
There was an error while loading. Please reload this page.