From 072d2a3e7ce06ded0f3b553c9358fa029c8cffb3 Mon Sep 17 00:00:00 2001 From: YG-Riku Date: Mon, 15 Aug 2016 19:25:33 +0900 Subject: [PATCH 1/3] BUG: iloc fails with non lex-sorted MultiIndex #13797 corrected how iloc handles tuple-keys for multiindex --- pandas/core/indexing.py | 2 +- pandas/tests/test_multilevel.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) mode change 100644 => 100755 pandas/core/indexing.py mode change 100644 => 100755 pandas/tests/test_multilevel.py diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py old mode 100644 new mode 100755 index a7cc3b9dddd36..91fe8cc1f73d9 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -903,7 +903,7 @@ def _getitem_lowerdim(self, tup): # we maybe be using a tuple to represent multiple dimensions here ax0 = self.obj._get_axis(0) - if isinstance(ax0, MultiIndex): + if isinstance(ax0, MultiIndex) and self.name != 'iloc': result = self._handle_lowerdim_multi_index_axis0(tup) if result is not None: return result diff --git a/pandas/tests/test_multilevel.py b/pandas/tests/test_multilevel.py old mode 100644 new mode 100755 index f3b0becccf596..0305801a46a23 --- a/pandas/tests/test_multilevel.py +++ b/pandas/tests/test_multilevel.py @@ -2420,6 +2420,25 @@ def test_repeat(self): m_df = pd.Series(data, index=m_idx) assert m_df.repeat(3).shape == (3 * len(data), ) + def test_iloc_mi(self): + # GH 13797 + + ind_nonLex = [ + ['CC', 'CC', 'CC', 'BB', 'BB'], + ['A', 'B', 'B', 'a', 'b'] + ] + + strCol = pd.DataFrame( + ['fooA', 'fooB', 'fooC', 'fooD', 'fooE']) + + dat = np.arange(1, 26).reshape(5, 5) + df = pd.concat([strCol, pd.DataFrame(dat)], axis=1) + df1 = pd.DataFrame(df.values, index=ind_nonLex) + + assert df1.iloc[0, 0] == 'fooA' + assert df1.iloc[4, 0] == 'fooE' + assert df1.iloc[4, 5] == 25 + if __name__ == '__main__': nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'], From a23c73615a82be50c202352fa7b7f53bf44720e5 Mon Sep 17 00:00:00 2001 From: YG-Riku Date: Sun, 11 Sep 2016 20:47:56 +0900 Subject: [PATCH 2/3] BUG: iloc fails with non lex-sorted MultiIndex #13797 added comments and modified test code --- pandas/core/indexing.py | 2 ++ pandas/tests/test_multilevel.py | 40 ++++++++++++++++++++------------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 91fe8cc1f73d9..35fcf0d49d0d6 100755 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -903,6 +903,8 @@ def _getitem_lowerdim(self, tup): # we maybe be using a tuple to represent multiple dimensions here ax0 = self.obj._get_axis(0) + # ...but iloc should handle the tuple as simple integer-location + # instead of checking it as multiindex representation (GH 13797) if isinstance(ax0, MultiIndex) and self.name != 'iloc': result = self._handle_lowerdim_multi_index_axis0(tup) if result is not None: diff --git a/pandas/tests/test_multilevel.py b/pandas/tests/test_multilevel.py index 0305801a46a23..58ac845bf8f2e 100755 --- a/pandas/tests/test_multilevel.py +++ b/pandas/tests/test_multilevel.py @@ -2422,22 +2422,30 @@ def test_repeat(self): def test_iloc_mi(self): # GH 13797 - - ind_nonLex = [ - ['CC', 'CC', 'CC', 'BB', 'BB'], - ['A', 'B', 'B', 'a', 'b'] - ] - - strCol = pd.DataFrame( - ['fooA', 'fooB', 'fooC', 'fooD', 'fooE']) - - dat = np.arange(1, 26).reshape(5, 5) - df = pd.concat([strCol, pd.DataFrame(dat)], axis=1) - df1 = pd.DataFrame(df.values, index=ind_nonLex) - - assert df1.iloc[0, 0] == 'fooA' - assert df1.iloc[4, 0] == 'fooE' - assert df1.iloc[4, 5] == 25 + # Test if iloc can handle integer locations in MultiIndexed DataFrame + + data =[ + ['str00', 'str01'], + ['str10', 'str11'], + ['str20', 'srt21'], + ['str30', 'str31'], + ['str40', 'str41'] + ] + + mi= pd.MultiIndex.from_tuples( + [('CC','A'), + ('CC','B'), + ('CC','B'), + ('BB','a'), + ('BB','b') + ]) + + ans = pd.DataFrame(data) + df_mi = pd.DataFrame(data, index = mi) + + res = pd.DataFrame([[df_mi.iloc[r,c] for c in range(2)] for r in range(5)]) + + assert_frame_equal(res, ans) if __name__ == '__main__': From 1cf86a184739191af54a28c6a2664b2166c49c78 Mon Sep 17 00:00:00 2001 From: YG-Riku Date: Mon, 12 Sep 2016 17:17:02 +0900 Subject: [PATCH 3/3] BUG: iloc fails with non lex-sorted MultiIndex #13797 apply pep8radius to tests/test_multilevel.py --- pandas/tests/test_multilevel.py | 35 +++++++++++++++++---------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/pandas/tests/test_multilevel.py b/pandas/tests/test_multilevel.py index 58ac845bf8f2e..370d7f720c0e0 100755 --- a/pandas/tests/test_multilevel.py +++ b/pandas/tests/test_multilevel.py @@ -2424,26 +2424,27 @@ def test_iloc_mi(self): # GH 13797 # Test if iloc can handle integer locations in MultiIndexed DataFrame - data =[ - ['str00', 'str01'], - ['str10', 'str11'], - ['str20', 'srt21'], - ['str30', 'str31'], - ['str40', 'str41'] - ] - - mi= pd.MultiIndex.from_tuples( - [('CC','A'), - ('CC','B'), - ('CC','B'), - ('BB','a'), - ('BB','b') - ]) + data = [ + ['str00', 'str01'], + ['str10', 'str11'], + ['str20', 'srt21'], + ['str30', 'str31'], + ['str40', 'str41'] + ] + + mi = pd.MultiIndex.from_tuples( + [('CC', 'A'), + ('CC', 'B'), + ('CC', 'B'), + ('BB', 'a'), + ('BB', 'b') + ]) ans = pd.DataFrame(data) - df_mi = pd.DataFrame(data, index = mi) + df_mi = pd.DataFrame(data, index=mi) - res = pd.DataFrame([[df_mi.iloc[r,c] for c in range(2)] for r in range(5)]) + res = pd.DataFrame([[df_mi.iloc[r, c] for c in range(2)] + for r in range(5)]) assert_frame_equal(res, ans)