Skip to content

Commit 622218e

Browse files
committed
Merge pull request #6065 from jreback/iloc_bug
BUG: suble iloc indexing bug with single block and multi-axis indexing (surfaced in GH6059)
2 parents 929fd1c + 17a7fa6 commit 622218e

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
@@ -141,6 +141,7 @@ Bug Fixes
141141
- Bug in setting using fancy indexing a single element with a non-scalar (e.g. a list),
142142
(:issue:`6043`)
143143
- Regression in ``.get(None)`` indexing from 0.12 (:issue:`5652`)
144+
- Subtle ``iloc`` indexing bug, surfaced in (:issue:`6059`)
144145

145146
pandas 0.13.0
146147
-------------

pandas/core/internals.py

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

25972597
if axis == 0:
25982598
new_items = new_axes[0]
2599+
2600+
# we want to preserver the view of a single-block
25992601
if len(self.blocks) == 1:
2602+
26002603
blk = self.blocks[0]
2604+
2605+
# see GH 6059
2606+
ref_locs = blk._ref_locs
2607+
if ref_locs is not None:
2608+
2609+
# need to preserve the ref_locs and just shift them
2610+
indexer = np.ones(len(ref_locs),dtype=bool)
2611+
indexer[slobj] = False
2612+
indexer = indexer.astype(int).cumsum()[slobj]
2613+
ref_locs = ref_locs[slobj]
2614+
ref_locs -= indexer
2615+
26012616
newb = make_block(blk._slice(slobj), new_items, new_items,
26022617
klass=blk.__class__, fastpath=True,
2603-
placement=blk._ref_locs)
2618+
placement=ref_locs)
2619+
26042620
new_blocks = [newb]
26052621
else:
26062622
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)