Skip to content
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

CLN: Clean tests for *.sort_index, *.sort_values and df.drop_duplicates #30651

Merged
merged 6 commits into from
Jan 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions pandas/tests/frame/methods/test_drop_duplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ def test_drop_duplicates_inplace():
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize("inplace", [True, False])
@pytest.mark.parametrize(
"origin_dict, output_dict, ignore_index, output_index",
[
Expand All @@ -403,24 +404,17 @@ def test_drop_duplicates_inplace():
],
)
def test_drop_duplicates_ignore_index(
origin_dict, output_dict, ignore_index, output_index
inplace, origin_dict, output_dict, ignore_index, output_index
):
# GH 30114
df = DataFrame(origin_dict)
expected = DataFrame(output_dict, index=output_index)

# Test when inplace is False
result = df.drop_duplicates(ignore_index=ignore_index)
tm.assert_frame_equal(result, expected)

# to verify original dataframe is not mutated
tm.assert_frame_equal(df, DataFrame(origin_dict))

# Test when inplace is True
copied_df = df.copy()

copied_df.drop_duplicates(ignore_index=ignore_index, inplace=True)
tm.assert_frame_equal(copied_df, expected)
if inplace:
result_df = df.copy()
result_df.drop_duplicates(ignore_index=ignore_index, inplace=inplace)
else:
result_df = df.drop_duplicates(ignore_index=ignore_index, inplace=inplace)

# to verify that input is unchanged
tm.assert_frame_equal(result_df, expected)
tm.assert_frame_equal(df, DataFrame(origin_dict))
51 changes: 29 additions & 22 deletions pandas/tests/frame/methods/test_sort_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ def test_sort_index_intervalindex(self):
result = result.columns.levels[1].categories
tm.assert_index_equal(result, expected)

@pytest.mark.parametrize("inplace", [True, False])
@pytest.mark.parametrize(
"original_dict, sorted_dict, ascending, ignore_index, output_index",
[
Expand All @@ -240,25 +241,28 @@ def test_sort_index_intervalindex(self):
],
)
def test_sort_index_ignore_index(
self, original_dict, sorted_dict, ascending, ignore_index, output_index
self, inplace, original_dict, sorted_dict, ascending, ignore_index, output_index
):
# GH 30114
original_index = [2, 5, 3]
df = DataFrame(original_dict, index=original_index)
expected_df = DataFrame(sorted_dict, index=output_index)

sorted_df = df.sort_index(ascending=ascending, ignore_index=ignore_index)
tm.assert_frame_equal(sorted_df, expected_df)
tm.assert_frame_equal(df, DataFrame(original_dict, index=original_index))

# Test when inplace is True
copied_df = df.copy()
copied_df.sort_index(
ascending=ascending, ignore_index=ignore_index, inplace=True
)
tm.assert_frame_equal(copied_df, expected_df)
kwargs = {
"ascending": ascending,
"ignore_index": ignore_index,
"inplace": inplace,
}

if inplace:
result_df = df.copy()
result_df.sort_index(**kwargs)
else:
result_df = df.sort_index(**kwargs)

tm.assert_frame_equal(result_df, expected_df)
tm.assert_frame_equal(df, DataFrame(original_dict, index=original_index))

@pytest.mark.parametrize("inplace", [True, False])
@pytest.mark.parametrize(
"original_dict, sorted_dict, ascending, ignore_index, output_index",
[
Expand Down Expand Up @@ -293,21 +297,24 @@ def test_sort_index_ignore_index(
],
)
def test_sort_index_ignore_index_multi_index(
self, original_dict, sorted_dict, ascending, ignore_index, output_index
self, inplace, original_dict, sorted_dict, ascending, ignore_index, output_index
):
# GH 30114, this is to test ignore_index on MulitIndex of index
mi = MultiIndex.from_tuples([[2, 1], [3, 4]], names=list("AB"))
df = DataFrame(original_dict, index=mi)
expected_df = DataFrame(sorted_dict, index=output_index)

sorted_df = df.sort_index(ascending=ascending, ignore_index=ignore_index)
tm.assert_frame_equal(sorted_df, expected_df)
tm.assert_frame_equal(df, DataFrame(original_dict, index=mi))
kwargs = {
"ascending": ascending,
"ignore_index": ignore_index,
"inplace": inplace,
}

# Test when inplace is True
copied_df = df.copy()
copied_df.sort_index(
ascending=ascending, ignore_index=ignore_index, inplace=True
)
tm.assert_frame_equal(copied_df, expected_df)
if inplace:
result_df = df.copy()
result_df.sort_index(**kwargs)
else:
result_df = df.sort_index(**kwargs)

tm.assert_frame_equal(result_df, expected_df)
tm.assert_frame_equal(df, DataFrame(original_dict, index=mi))
23 changes: 9 additions & 14 deletions pandas/tests/frame/methods/test_sort_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ def test_sort_values_na_position_with_categories_raises(self):
with pytest.raises(ValueError):
df.sort_values(by="c", ascending=False, na_position="bad_position")

@pytest.mark.parametrize("inplace", [True, False])
@pytest.mark.parametrize(
"original_dict, sorted_dict, ignore_index, output_index",
[
Expand All @@ -481,24 +482,18 @@ def test_sort_values_na_position_with_categories_raises(self):
],
)
def test_sort_values_ignore_index(
self, original_dict, sorted_dict, ignore_index, output_index
self, inplace, original_dict, sorted_dict, ignore_index, output_index
):
# GH 30114
df = DataFrame(original_dict)
expected = DataFrame(sorted_dict, index=output_index)
kwargs = {"ignore_index": ignore_index, "inplace": inplace}

# Test when inplace is False
sorted_df = df.sort_values("A", ascending=False, ignore_index=ignore_index)
tm.assert_frame_equal(sorted_df, expected)

tm.assert_frame_equal(df, DataFrame(original_dict))

# Test when inplace is True
copied_df = df.copy()

copied_df.sort_values(
"A", ascending=False, ignore_index=ignore_index, inplace=True
)
tm.assert_frame_equal(copied_df, expected)
if inplace:
result_df = df.copy()
result_df.sort_values("A", ascending=False, **kwargs)
else:
result_df = df.sort_values("A", ascending=False, **kwargs)

tm.assert_frame_equal(result_df, expected)
tm.assert_frame_equal(df, DataFrame(original_dict))
30 changes: 15 additions & 15 deletions pandas/tests/series/methods/test_sort_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def test_sort_index_intervals(self):
)
tm.assert_series_equal(result, expected)

@pytest.mark.parametrize("inplace", [True, False])
@pytest.mark.parametrize(
"original_list, sorted_list, ascending, ignore_index, output_index",
[
Expand All @@ -146,23 +147,22 @@ def test_sort_index_intervals(self):
],
)
def test_sort_index_ignore_index(
self, original_list, sorted_list, ascending, ignore_index, output_index
self, inplace, original_list, sorted_list, ascending, ignore_index, output_index
):
# GH 30114
ser = Series(original_list)
expected = Series(sorted_list, index=output_index)

# Test when inplace is False
sorted_sr = ser.sort_index(ascending=ascending, ignore_index=ignore_index)
tm.assert_series_equal(sorted_sr, expected)

tm.assert_series_equal(ser, Series(original_list))

# Test when inplace is True
copied_sr = ser.copy()
copied_sr.sort_index(
ascending=ascending, ignore_index=ignore_index, inplace=True
)
tm.assert_series_equal(copied_sr, expected)

kwargs = {
"ascending": ascending,
"ignore_index": ignore_index,
"inplace": inplace,
}

if inplace:
result_ser = ser.copy()
result_ser.sort_index(**kwargs)
else:
result_ser = ser.sort_index(**kwargs)

tm.assert_series_equal(result_ser, expected)
tm.assert_series_equal(ser, Series(original_list))
24 changes: 11 additions & 13 deletions pandas/tests/series/methods/test_sort_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ def test_sort_values_categorical(self):
expected = df.iloc[[2, 1, 5, 4, 3, 0]]
tm.assert_frame_equal(result, expected)

@pytest.mark.parametrize("inplace", [True, False])
@pytest.mark.parametrize(
"original_list, sorted_list, ignore_index, output_index",
[
Expand All @@ -165,21 +166,18 @@ def test_sort_values_categorical(self):
],
)
def test_sort_values_ignore_index(
self, original_list, sorted_list, ignore_index, output_index
self, inplace, original_list, sorted_list, ignore_index, output_index
):
# GH 30114
sr = Series(original_list)
ser = Series(original_list)
expected = Series(sorted_list, index=output_index)
kwargs = {"ignore_index": ignore_index, "inplace": inplace}

# Test when inplace is False
sorted_sr = sr.sort_values(ascending=False, ignore_index=ignore_index)
tm.assert_series_equal(sorted_sr, expected)
if inplace:
result_ser = ser.copy()
result_ser.sort_values(ascending=False, **kwargs)
else:
result_ser = ser.sort_values(ascending=False, **kwargs)

tm.assert_series_equal(sr, Series(original_list))

# Test when inplace is True
copied_sr = sr.copy()
copied_sr.sort_values(ascending=False, ignore_index=ignore_index, inplace=True)
tm.assert_series_equal(copied_sr, expected)

tm.assert_series_equal(sr, Series(original_list))
tm.assert_series_equal(result_ser, expected)
tm.assert_series_equal(ser, Series(original_list))