Skip to content

Commit

Permalink
Testcase Subtracting two series with unordered index and all-nan inde…
Browse files Browse the repository at this point in the history
…x produces unexpected result (pandas-dev#45900)

* add test

* autopep

* autopep

* add

* add
  • Loading branch information
Khor Chean Wei authored and phofl committed Feb 14, 2022
1 parent 43b989a commit 5180d32
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions pandas/tests/indexing/multiindex/test_multiindex.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import pytest

import pandas._libs.index as _index
from pandas.errors import PerformanceWarning
Expand Down Expand Up @@ -149,3 +150,66 @@ def test_rename_multiindex_with_duplicates(self):
mi2 = MultiIndex.from_tuples([("Apple", "cat"), ("B", "cat"), ("B", "cat")])
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.0, np.nan), (np.nan, np.nan)],
[(81.0, np.nan), (np.nan, np.nan)],
[1, 2],
[1, 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), (np.nan, np.nan)],
[(81.0, np.nan), (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
):
# GH 38439
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_tuples(data_expected[0])
b_index_expected = MultiIndex.from_tuples(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)
a_series_expected.index = a_series_expected.index.set_levels(
[
a_series_expected.index.levels[0].astype("float"),
a_series_expected.index.levels[1].astype("float"),
]
)
b_series_expected.index = b_series_expected.index.set_levels(
[
b_series_expected.index.levels[0].astype("float"),
b_series_expected.index.levels[1].astype("float"),
]
)

tm.assert_series_equal(result[0], a_series_expected)
tm.assert_series_equal(result[1], b_series_expected)

0 comments on commit 5180d32

Please sign in to comment.