From ea55a095b50e34822d2860816c9d98ca184995f7 Mon Sep 17 00:00:00 2001 From: gamenerd457 <72939103+gamenerd457@users.noreply.github.com> Date: Mon, 4 Oct 2021 20:16:11 +0530 Subject: [PATCH 01/11] added test case for issue #43599 --- pandas/tests/indexing/test_loc.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 25c0625d1d790..08dcf2ac7fee2 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -38,6 +38,7 @@ from pandas.api.types import is_scalar from pandas.core.api import Float64Index from pandas.tests.indexing.common import Base +import unittest class TestLoc(Base): @@ -2833,7 +2834,7 @@ def test_loc_assign_dict_to_row(self, dtype): @td.skip_array_manager_invalid_test def test_loc_setitem_dict_timedelta_multiple_set(self): - # GH 16309 + # GH 16309s result = DataFrame(columns=["time", "value"]) result.loc[1] = {"time": Timedelta(6, unit="s"), "value": "foo"} result.loc[1] = {"time": Timedelta(6, unit="s"), "value": "foo"} @@ -2857,3 +2858,15 @@ def test_loc_set_multiple_items_in_multiple_new_columns(self): ) tm.assert_frame_equal(df, expected) + + +class TestLoc3(unittest.TestCase): #test case for issue #43599 + def test_loc_multiindex(self): + df = pd.DataFrame(index=pd.MultiIndex.from_product([list('abc'), list('de'), list('f')]), columns=['Val']) + df1=df.loc[np.s_[:,'d',:]] + df2=pd.DataFrame(index=pd.MultiIndex.from_product([list('abc'), list('d'), list('f')]), columns=['Val']) + + + tm.assert_frame_equal(df1,df2) + + From c317099878cd6f49e6433ab299fe9192de551d82 Mon Sep 17 00:00:00 2001 From: adithyan_sukumar <72939103+gamenerd457@users.noreply.github.com> Date: Mon, 4 Oct 2021 21:32:20 +0530 Subject: [PATCH 02/11] Update test_loc.py --- pandas/tests/indexing/test_loc.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 08dcf2ac7fee2..c5c2c1105b9a0 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -2834,7 +2834,7 @@ def test_loc_assign_dict_to_row(self, dtype): @td.skip_array_manager_invalid_test def test_loc_setitem_dict_timedelta_multiple_set(self): - # GH 16309s + # GH 16309 result = DataFrame(columns=["time", "value"]) result.loc[1] = {"time": Timedelta(6, unit="s"), "value": "foo"} result.loc[1] = {"time": Timedelta(6, unit="s"), "value": "foo"} @@ -2858,15 +2858,12 @@ def test_loc_set_multiple_items_in_multiple_new_columns(self): ) tm.assert_frame_equal(df, expected) - - -class TestLoc3(unittest.TestCase): #test case for issue #43599 +class TestLoc3(unittest.TestCase): + #test case for issue #43599 def test_loc_multiindex(self): - df = pd.DataFrame(index=pd.MultiIndex.from_product([list('abc'), list('de'), list('f')]), columns=['Val']) + df = DataFrame(index=MultiIndex.from_product([list('abc'), list('de'), list('f')]), columns=['Val']) df1=df.loc[np.s_[:,'d',:]] - df2=pd.DataFrame(index=pd.MultiIndex.from_product([list('abc'), list('d'), list('f')]), columns=['Val']) - - + df2=DataFrame(index=MultiIndex.from_product([list('abc'), list('d'), list('f')]), columns=['Val']) tm.assert_frame_equal(df1,df2) From 349ece90f7780f306da474f6ea0fa569e1f9f31a Mon Sep 17 00:00:00 2001 From: adithyan_sukumar <72939103+gamenerd457@users.noreply.github.com> Date: Tue, 5 Oct 2021 18:41:11 +0530 Subject: [PATCH 03/11] used pytest --- pandas/tests/indexing/test_loc.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index c5c2c1105b9a0..fc04180a3426e 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -38,7 +38,7 @@ from pandas.api.types import is_scalar from pandas.core.api import Float64Index from pandas.tests.indexing.common import Base -import unittest + class TestLoc(Base): @@ -2858,12 +2858,9 @@ def test_loc_set_multiple_items_in_multiple_new_columns(self): ) tm.assert_frame_equal(df, expected) -class TestLoc3(unittest.TestCase): - #test case for issue #43599 - def test_loc_multiindex(self): - df = DataFrame(index=MultiIndex.from_product([list('abc'), list('de'), list('f')]), columns=['Val']) - df1=df.loc[np.s_[:,'d',:]] - df2=DataFrame(index=MultiIndex.from_product([list('abc'), list('d'), list('f')]), columns=['Val']) - tm.assert_frame_equal(df1,df2) - +def test_loc_multiindex(): + df = pd.DataFrame(index=pd.MultiIndex.from_product([list('abc'), list('de'), list('f')]), columns=['Val']) + df1=df.loc[np.s_[:,'d',:]] + df2=pd.DataFrame(index=pd.MultiIndex.from_product([list('abc'), list('d'), list('f')]), columns=['Val']) + tm.assert_frame_equal(df1,df2) From f001462216ce74f63ae1c23cb6e6e9bff3413d0d Mon Sep 17 00:00:00 2001 From: adithyan_sukumar <72939103+gamenerd457@users.noreply.github.com> Date: Tue, 5 Oct 2021 18:52:25 +0530 Subject: [PATCH 04/11] updated style --- pandas/tests/indexing/test_loc.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index fc04180a3426e..d89e853e37dbc 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -2858,9 +2858,15 @@ def test_loc_set_multiple_items_in_multiple_new_columns(self): ) tm.assert_frame_equal(df, expected) + + def test_loc_multiindex(): - df = pd.DataFrame(index=pd.MultiIndex.from_product([list('abc'), list('de'), list('f')]), columns=['Val']) - df1=df.loc[np.s_[:,'d',:]] - df2=pd.DataFrame(index=pd.MultiIndex.from_product([list('abc'), list('d'), list('f')]), columns=['Val']) + df = DataFrame( + index = MultiIndex.from_product([list('abc'), list('de'), list('f')]), columns = ['Val'] + ) + df1 = df.loc[np.s_[:, 'd', :]] + df2 = DataFrame( + index = MultiIndex.from_product([list('abc'), list('d'), list('f')]), columns = ['Val'] + ) tm.assert_frame_equal(df1,df2) From 663da78a85c8c982583603e0a99452b7c286e333 Mon Sep 17 00:00:00 2001 From: adithyan_sukumar <72939103+gamenerd457@users.noreply.github.com> Date: Tue, 5 Oct 2021 18:59:43 +0530 Subject: [PATCH 05/11] Update test_loc.py --- pandas/tests/indexing/test_loc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index d89e853e37dbc..39e3ac153fa45 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -2868,5 +2868,5 @@ def test_loc_multiindex(): df2 = DataFrame( index = MultiIndex.from_product([list('abc'), list('d'), list('f')]), columns = ['Val'] ) - tm.assert_frame_equal(df1,df2) + tm.assert_frame_equal(df1, df2) From e72a2e98fdfe5890d888cb97dce0a87ed1d24995 Mon Sep 17 00:00:00 2001 From: adithyan_sukumar <72939103+gamenerd457@users.noreply.github.com> Date: Tue, 5 Oct 2021 19:13:41 +0530 Subject: [PATCH 06/11] Update test_loc.py --- pandas/tests/indexing/test_loc.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 39e3ac153fa45..3e8f5ddd2f691 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -2862,11 +2862,13 @@ def test_loc_set_multiple_items_in_multiple_new_columns(self): def test_loc_multiindex(): df = DataFrame( - index = MultiIndex.from_product([list('abc'), list('de'), list('f')]), columns = ['Val'] + index = MultiIndex.from_product([list("abc"), list("de"), list("f")]), + columns = ["Val"], ) - df1 = df.loc[np.s_[:, 'd', :]] + df1 = df.loc[np.s_[:, "d", :]] df2 = DataFrame( - index = MultiIndex.from_product([list('abc'), list('d'), list('f')]), columns = ['Val'] + index = MultiIndex.from_product([list("abc"), list("d"), list("f")]), + columns = ["Val"], ) tm.assert_frame_equal(df1, df2) From b50540b816158ebfa4a87b40c8c6c5d60ae22c10 Mon Sep 17 00:00:00 2001 From: adithyan_sukumar <72939103+gamenerd457@users.noreply.github.com> Date: Sat, 9 Oct 2021 19:31:17 +0530 Subject: [PATCH 07/11] Update test_loc.py --- pandas/tests/indexing/test_loc.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 3e8f5ddd2f691..69ff06cb0c174 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -2866,9 +2866,6 @@ def test_loc_multiindex(): columns = ["Val"], ) df1 = df.loc[np.s_[:, "d", :]] - df2 = DataFrame( - index = MultiIndex.from_product([list("abc"), list("d"), list("f")]), - columns = ["Val"], - ) + df2 = df.loc[np.s_[:, "d", :]] tm.assert_frame_equal(df1, df2) From 1e20e026d7e35dec8883a16078054bb1336ecd2d Mon Sep 17 00:00:00 2001 From: adithyan_sukumar <72939103+gamenerd457@users.noreply.github.com> Date: Sat, 9 Oct 2021 20:10:23 +0530 Subject: [PATCH 08/11] Update test_loc.py --- pandas/tests/indexing/test_loc.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 69ff06cb0c174..19613c409b8f1 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -2862,8 +2862,8 @@ def test_loc_set_multiple_items_in_multiple_new_columns(self): def test_loc_multiindex(): df = DataFrame( - index = MultiIndex.from_product([list("abc"), list("de"), list("f")]), - columns = ["Val"], + index=MultiIndex.from_product([list("abc"), list("de"), list("f")]), + columns=["Val"], ) df1 = df.loc[np.s_[:, "d", :]] df2 = df.loc[np.s_[:, "d", :]] From 6715c711d0af149e5f60f908a28baaa5d5533d6b Mon Sep 17 00:00:00 2001 From: adithyan_sukumar <72939103+gamenerd457@users.noreply.github.com> Date: Tue, 12 Oct 2021 12:36:49 +0530 Subject: [PATCH 09/11] made the requested changes --- pandas/tests/indexing/test_loc.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 19613c409b8f1..ba1488ead1950 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -2865,7 +2865,10 @@ def test_loc_multiindex(): index=MultiIndex.from_product([list("abc"), list("de"), list("f")]), columns=["Val"], ) - df1 = df.loc[np.s_[:, "d", :]] - df2 = df.loc[np.s_[:, "d", :]] - tm.assert_frame_equal(df1, df2) + result = df.loc[np.s_[:, "d", :]] + expected = DataFrame( + index=MultiIndex.from_product([list("abc"), list("d"), list("f")]), + columns=["Val"], + ) + tm.assert_frame_equal(result, expected) From ddeb35744c9664bdd8ee9a5029fa1f7acf5ea99e Mon Sep 17 00:00:00 2001 From: adithyan_sukumar <72939103+gamenerd457@users.noreply.github.com> Date: Tue, 12 Oct 2021 12:41:23 +0530 Subject: [PATCH 10/11] Update test_loc.py --- pandas/tests/indexing/multiindex/test_loc.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pandas/tests/indexing/multiindex/test_loc.py b/pandas/tests/indexing/multiindex/test_loc.py index 104fa2da7a67e..cf2d27c6f580c 100644 --- a/pandas/tests/indexing/multiindex/test_loc.py +++ b/pandas/tests/indexing/multiindex/test_loc.py @@ -912,3 +912,14 @@ def test_loc_keyerror_rightmost_key_missing(): df = df.set_index(["A", "B"]) with pytest.raises(KeyError, match="^1$"): df.loc[(100, 1)] +def test_loc_multiindex(): + df = DataFrame( + index=MultiIndex.from_product([list("abc"), list("de"), list("f")]), + columns=["Val"], + ) + result = df.loc[np.s_[:, "d", :]] + expected = DataFrame( + index=MultiIndex.from_product([list("abc"), list("d"), list("f")]), + columns=["Val"], + ) + tm.assert_frame_equal(result, expected) From 0f772a6504ce7f0cc715549fcda650e861e76cb9 Mon Sep 17 00:00:00 2001 From: adithyan_sukumar <72939103+gamenerd457@users.noreply.github.com> Date: Tue, 12 Oct 2021 12:42:14 +0530 Subject: [PATCH 11/11] Create test_loc.py --- pandas/tests/indexing/test_loc.py | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index ba1488ead1950..2f5bf30972967 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -2860,15 +2860,5 @@ def test_loc_set_multiple_items_in_multiple_new_columns(self): tm.assert_frame_equal(df, expected) -def test_loc_multiindex(): - df = DataFrame( - index=MultiIndex.from_product([list("abc"), list("de"), list("f")]), - columns=["Val"], - ) - result = df.loc[np.s_[:, "d", :]] - expected = DataFrame( - index=MultiIndex.from_product([list("abc"), list("d"), list("f")]), - columns=["Val"], - ) - tm.assert_frame_equal(result, expected) +