Skip to content

Commit 17a7fa6

Browse files
committed
BUG: suble iloc indexing bug with single block and multi-axis indexing
1 parent c9013b8 commit 17a7fa6

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

doc/source/release.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ Bug Fixes
140140
- Bug in setting using fancy indexing a single element with a non-scalar (e.g. a list),
141141
(:issue:`6043`)
142142
- Regression in ``.get(None)`` indexing from 0.12 (:issue:`5652`)
143+
- Subtle ``iloc`` indexing bug, surfaced in (:issue:`6059`)
143144

144145
pandas 0.13.0
145146
-------------

pandas/core/internals.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2586,11 +2586,27 @@ def get_slice(self, slobj, axis=0, raise_on_error=False):
25862586

25872587
if axis == 0:
25882588
new_items = new_axes[0]
2589+
2590+
# we want to preserver the view of a single-block
25892591
if len(self.blocks) == 1:
2592+
25902593
blk = self.blocks[0]
2594+
2595+
# see GH 6059
2596+
ref_locs = blk._ref_locs
2597+
if ref_locs is not None:
2598+
2599+
# need to preserve the ref_locs and just shift them
2600+
indexer = np.ones(len(ref_locs),dtype=bool)
2601+
indexer[slobj] = False
2602+
indexer = indexer.astype(int).cumsum()[slobj]
2603+
ref_locs = ref_locs[slobj]
2604+
ref_locs -= indexer
2605+
25912606
newb = make_block(blk._slice(slobj), new_items, new_items,
25922607
klass=blk.__class__, fastpath=True,
2593-
placement=blk._ref_locs)
2608+
placement=ref_locs)
2609+
25942610
new_blocks = [newb]
25952611
else:
25962612
return self.reindex_items(

pandas/tests/test_indexing.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,49 @@ def test_iloc_getitem_frame(self):
720720
# trying to use a label
721721
self.assertRaises(ValueError, df.iloc.__getitem__, tuple(['j','D']))
722722

723+
724+
def test_iloc_getitem_doc_issue(self):
725+
726+
# multi axis slicing issue with single block
727+
# surfaced in GH 6059
728+
729+
arr = np.random.randn(6,4)
730+
index = date_range('20130101',periods=6)
731+
columns = list('ABCD')
732+
df = DataFrame(arr,index=index,columns=columns)
733+
734+
# defines ref_locs
735+
df.describe()
736+
737+
result = df.iloc[3:5,0:2]
738+
str(result)
739+
result.dtypes
740+
741+
expected = DataFrame(arr[3:5,0:2],index=index[3:5],columns=columns[0:2])
742+
assert_frame_equal(result,expected)
743+
744+
# for dups
745+
df.columns = list('aaaa')
746+
result = df.iloc[3:5,0:2]
747+
str(result)
748+
result.dtypes
749+
750+
expected = DataFrame(arr[3:5,0:2],index=index[3:5],columns=list('aa'))
751+
assert_frame_equal(result,expected)
752+
753+
# related
754+
arr = np.random.randn(6,4)
755+
index = list(range(0,12,2))
756+
columns = list(range(0,8,2))
757+
df = DataFrame(arr,index=index,columns=columns)
758+
759+
df._data.blocks[0].ref_locs
760+
result = df.iloc[1:5,2:4]
761+
str(result)
762+
result.dtypes
763+
expected = DataFrame(arr[1:5,2:4],index=index[1:5],columns=columns[2:4])
764+
assert_frame_equal(result,expected)
765+
723766
def test_setitem_ndarray_1d(self):
724767
# GH5508
725768

0 commit comments

Comments
 (0)