Skip to content

CLN/TST: test_series_align_multiindex_containing_nan #50516

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

Merged
merged 3 commits into from
Jan 4, 2023
Merged
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
78 changes: 28 additions & 50 deletions pandas/tests/indexing/multiindex/test_multiindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,57 +151,35 @@ def test_rename_multiindex_with_duplicates(self):
expected = DataFrame(index=mi2)
tm.assert_frame_equal(df, expected)

@pytest.mark.parametrize(
"data_result, data_expected",
[
(
[
[(81.0, np.nan), (np.nan, np.nan)],
[(np.nan, np.nan), (82.0, np.nan)],
[1, 2],
[1, 2],
],
[
[[81, 82.0, np.nan], Series([np.nan, np.nan, np.nan])],
[[81, 82.0, np.nan], Series([np.nan, np.nan, np.nan])],
[1, np.nan, 2],
[np.nan, 2, 1],
],
),
(
[
[(81.0, np.nan), (np.nan, np.nan)],
[(np.nan, np.nan), (81.0, np.nan)],
[1, 2],
[1, 2],
],
[
[[81.0, np.nan], Series([np.nan, np.nan])],
[[81.0, np.nan], Series([np.nan, np.nan])],
[1, 2],
[2, 1],
],
),
],
)
def test_subtracting_two_series_with_unordered_index_and_all_nan_index(
self, data_result, data_expected
):
def test_series_align_multiindex_with_nan_overlap_only(self):
# GH 38439
mi1 = MultiIndex.from_arrays([[81.0, np.nan], [np.nan, np.nan]])
mi2 = MultiIndex.from_arrays([[np.nan, 82.0], [np.nan, np.nan]])
ser1 = Series([1, 2], index=mi1)
ser2 = Series([1, 2], index=mi2)
result1, result2 = ser1.align(ser2)

mi = MultiIndex.from_arrays([[81.0, 82.0, np.nan], [np.nan, np.nan, np.nan]])
expected1 = Series([1.0, np.nan, 2.0], index=mi)
expected2 = Series([np.nan, 2.0, 1.0], index=mi)

tm.assert_series_equal(result1, expected1)
tm.assert_series_equal(result2, expected2)

def test_series_align_multiindex_with_nan(self):
# GH 38439
# TODO: Refactor. This is impossible to understand GH#49443
a_index_result = MultiIndex.from_tuples(data_result[0])
b_index_result = MultiIndex.from_tuples(data_result[1])
a_series_result = Series(data_result[2], index=a_index_result)
b_series_result = Series(data_result[3], index=b_index_result)
result = a_series_result.align(b_series_result)

a_index_expected = MultiIndex.from_arrays(data_expected[0])
b_index_expected = MultiIndex.from_arrays(data_expected[1])
a_series_expected = Series(data_expected[2], index=a_index_expected)
b_series_expected = Series(data_expected[3], index=b_index_expected)

tm.assert_series_equal(result[0], a_series_expected)
tm.assert_series_equal(result[1], b_series_expected)
mi1 = MultiIndex.from_arrays([[81.0, np.nan], [np.nan, np.nan]])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the issue, I think this block should be a separate test

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, updated to split into two tests.

mi2 = MultiIndex.from_arrays([[np.nan, 81.0], [np.nan, np.nan]])
ser1 = Series([1, 2], index=mi1)
ser2 = Series([1, 2], index=mi2)
result1, result2 = ser1.align(ser2)

mi = MultiIndex.from_arrays([[81.0, np.nan], [np.nan, np.nan]])
expected1 = Series([1, 2], index=mi)
expected2 = Series([2, 1], index=mi)

tm.assert_series_equal(result1, expected1)
tm.assert_series_equal(result2, expected2)

def test_nunique_smoke(self):
# GH 34019
Expand Down