From 67e02b3f584accee627b20ebbf2138aa8d2bc7b8 Mon Sep 17 00:00:00 2001 From: Nick Eubank Date: Wed, 16 Dec 2015 11:20:38 -0800 Subject: [PATCH 1/2] first pass at fixing --- pandas/core/frame.py | 1 + pandas/core/indexing.py | 19 +++---------------- pandas/tests/test_frame.py | 1 + pandas/tests/test_indexing.py | 8 ++++++++ 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index dd41075ddf92e..1f29d477ee20b 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -217,6 +217,7 @@ def __init__(self, data=None, index=None, columns=None, dtype=None, dtype = self._validate_dtype(dtype) if isinstance(data, DataFrame): + data = data.iloc[:,:] data = data._data if isinstance(data, BlockManager): diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 9df72053fb0af..0492b6ab602b4 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -752,8 +752,8 @@ def _getitem_tuple(self, tup): if i >= self.obj.ndim: raise IndexingError('Too many indexers') - if is_null_slice(key): - continue + #if is_null_slice(key): + # continue retval = getattr(retval, self.name)._getitem_axis(key, axis=i) @@ -1171,8 +1171,6 @@ def _tuplify(self, loc): def _get_slice_axis(self, slice_obj, axis=0): obj = self.obj - if not need_slice(slice_obj): - return obj indexer = self._convert_slice_indexer(slice_obj, axis) if isinstance(indexer, slice): @@ -1244,8 +1242,7 @@ def _getbool_axis(self, key, axis=0): def _get_slice_axis(self, slice_obj, axis=0): """ this is pretty simple as we just have to deal with labels """ obj = self.obj - if not need_slice(slice_obj): - return obj + labels = obj._get_axis(axis) indexer = labels.slice_indexer(slice_obj.start, slice_obj.stop, @@ -1477,10 +1474,6 @@ def _getitem_tuple(self, tup): return retval def _get_slice_axis(self, slice_obj, axis=0): - obj = self.obj - - if not need_slice(slice_obj): - return obj slice_obj = self._convert_slice_indexer(slice_obj, axis) if isinstance(slice_obj, slice): @@ -1792,12 +1785,6 @@ def is_label_like(key): return not isinstance(key, slice) and not is_list_like_indexer(key) -def need_slice(obj): - return (obj.start is not None or - obj.stop is not None or - (obj.step is not None and obj.step != 1)) - - def maybe_droplevels(index, key): # drop levels original_index = index diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 09de3bf4a8046..5d9ce5de92ef5 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -2637,6 +2637,7 @@ def test_constructor_dtype_copy(self): def test_constructor_dtype_nocast_view(self): df = DataFrame([[1, 2]]) should_be_view = DataFrame(df, dtype=df[0].dtype) + self.assertTrue(should_be_view._is_view) should_be_view[0][0] = 99 self.assertEqual(df.values[0, 0], 99) diff --git a/pandas/tests/test_indexing.py b/pandas/tests/test_indexing.py index c6d80a08ad61a..e2635c216cc2c 100644 --- a/pandas/tests/test_indexing.py +++ b/pandas/tests/test_indexing.py @@ -5196,6 +5196,14 @@ def test_boolean_selection(self): self.assertRaises(TypeError, lambda : df4[df4.index < 2]) self.assertRaises(TypeError, lambda : df4[df4.index > 1]) + def test_empty_indexers_return_view(self): + df = pd.DataFrame({'col1':range(10,20), + 'col2':range(20,30)}) + self.assertTrue(df.loc[:,:]._is_view) + self.assertTrue(df.iloc[:,:]._is_view) + self.assertTrue(df.ix[:,:]._is_view) + + class TestSeriesNoneCoercion(tm.TestCase): EXPECTED_RESULTS = [ # For numeric series, we should coerce to NaN. From 47ae5166551ac92c0375253b3f8018fc4a46031a Mon Sep 17 00:00:00 2001 From: Nick Eubank Date: Wed, 16 Dec 2015 11:42:25 -0800 Subject: [PATCH 2/2] fixed iloc issue --- pandas/core/indexing.py | 9 +++------ pandas/tests/test_indexing.py | 15 ++++++++------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 0492b6ab602b4..1a7636cca39b0 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -752,9 +752,6 @@ def _getitem_tuple(self, tup): if i >= self.obj.ndim: raise IndexingError('Too many indexers') - #if is_null_slice(key): - # continue - retval = getattr(retval, self.name)._getitem_axis(key, axis=i) return retval @@ -1458,9 +1455,9 @@ def _getitem_tuple(self, tup): if i >= self.obj.ndim: raise IndexingError('Too many indexers') - if is_null_slice(key): - axis += 1 - continue + #if is_null_slice(key): + # axis += 1 + # continue retval = getattr(retval, self.name)._getitem_axis(key, axis=axis) diff --git a/pandas/tests/test_indexing.py b/pandas/tests/test_indexing.py index e2635c216cc2c..0cebaab2f2a3c 100644 --- a/pandas/tests/test_indexing.py +++ b/pandas/tests/test_indexing.py @@ -4912,6 +4912,14 @@ def test_maybe_numeric_slice(self): expected = [1] self.assertEqual(result, expected) + def test_empty_indexers_return_view(self): + # Closes Issue 11814 + df = pd.DataFrame({'col1':range(10,20), + 'col2':range(20,30)}) + self.assertTrue(df.loc[:,:]._is_view) + self.assertTrue(df.iloc[:,:]._is_view) + self.assertTrue(df.ix[:,:]._is_view) + class TestCategoricalIndex(tm.TestCase): @@ -5196,13 +5204,6 @@ def test_boolean_selection(self): self.assertRaises(TypeError, lambda : df4[df4.index < 2]) self.assertRaises(TypeError, lambda : df4[df4.index > 1]) - def test_empty_indexers_return_view(self): - df = pd.DataFrame({'col1':range(10,20), - 'col2':range(20,30)}) - self.assertTrue(df.loc[:,:]._is_view) - self.assertTrue(df.iloc[:,:]._is_view) - self.assertTrue(df.ix[:,:]._is_view) - class TestSeriesNoneCoercion(tm.TestCase): EXPECTED_RESULTS = [