From 5180d32c5d4843ef79eb3832e9372392fb3696da Mon Sep 17 00:00:00 2001 From: Khor Chean Wei Date: Sun, 13 Feb 2022 02:29:55 +0800 Subject: [PATCH] Testcase Subtracting two series with unordered index and all-nan index produces unexpected result (#45900) * add test * autopep * autopep * add * add --- .../indexing/multiindex/test_multiindex.py | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/pandas/tests/indexing/multiindex/test_multiindex.py b/pandas/tests/indexing/multiindex/test_multiindex.py index 9fa873a212cbd7..b88c411636610b 100644 --- a/pandas/tests/indexing/multiindex/test_multiindex.py +++ b/pandas/tests/indexing/multiindex/test_multiindex.py @@ -1,4 +1,5 @@ import numpy as np +import pytest import pandas._libs.index as _index from pandas.errors import PerformanceWarning @@ -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)