From 5daffa8a240f165cdaf74f70c5cb48fc4a0aa128 Mon Sep 17 00:00:00 2001 From: Margaret Sy Date: Mon, 22 May 2017 16:58:39 -0700 Subject: [PATCH] iloc and loc return new object when slice is a tuple --- pandas/core/indexing.py | 5 ++--- pandas/tests/indexing/test_iloc.py | 2 ++ pandas/tests/indexing/test_loc.py | 2 ++ 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index d5ef047ef9d8a..658adf5a943c9 100755 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -845,7 +845,7 @@ def _getitem_tuple(self, tup): return self._multi_take(tup) # no shortcut needed - retval = self.obj + retval = self.obj.copy() for i, key in enumerate(tup): if i >= self.obj.ndim: raise IndexingError('Too many indexers') @@ -854,7 +854,6 @@ def _getitem_tuple(self, tup): continue retval = getattr(retval, self.name)._getitem_axis(key, axis=i) - return retval def _multi_take_opportunity(self, tup): @@ -1665,7 +1664,7 @@ def _getitem_tuple(self, tup): except: pass - retval = self.obj + retval = self.obj.copy() axis = 0 for i, key in enumerate(tup): if i >= self.obj.ndim: diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index 9f647819438fa..70c7f673adf84 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -596,3 +596,5 @@ def test_loc_identity_slice_returns_new_object(self): # GH13873 df = DataFrame({'a': [1, 3, 5], 'b': [2, 4, 6]}) assert not df.iloc[:] is df + assert not df.iloc[:,:] is df + assert not df.iloc[pd.IndexSlice[:, :]] is df diff --git a/pandas/tests/indexing/test_loc.py b/pandas/tests/indexing/test_loc.py index 4c7250707da86..d86cf1fbc5520 100644 --- a/pandas/tests/indexing/test_loc.py +++ b/pandas/tests/indexing/test_loc.py @@ -635,3 +635,5 @@ def test_loc_identity_slice_returns_new_object(self): # GH13873 df = DataFrame({'a': [1, 3, 5], 'b': [2, 4, 6]}) assert not df.loc[:] is df + assert not df.loc[:,:] is df + assert not df.loc[pd.IndexSlice[:, :]] is df