Skip to content

Commit 4090a85

Browse files
committed
Merge pull request #3618 from jreback/iloc_broken
BUG: (GH3617) Fix indexing issue with ndim >= 3 with iloc
2 parents c0181d1 + c6c0b8e commit 4090a85

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

RELEASE.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ pandas 0.11.1
121121
- Fix incorrect dtype on groupby with ``as_index=False`` (GH3610_)
122122
- Fix ``read_csv`` to correctly encode identical na_values, e.g. ``na_values=[-999.0,-999]``
123123
was failing (GH3611_)
124+
- Fix indexing issue in ndim >= 3 with ``iloc`` (GH3617_)
124125

125126
.. _GH3164: https://github.com/pydata/pandas/issues/3164
126127
.. _GH2786: https://github.com/pydata/pandas/issues/2786
@@ -168,6 +169,7 @@ pandas 0.11.1
168169
.. _GH3590: https://github.com/pydata/pandas/issues/3590
169170
.. _GH3610: https://github.com/pydata/pandas/issues/3610
170171
.. _GH3596: https://github.com/pydata/pandas/issues/3596
172+
.. _GH3617: https://github.com/pydata/pandas/issues/3617
171173
.. _GH3435: https://github.com/pydata/pandas/issues/3435
172174
.. _GH3611: https://github.com/pydata/pandas/issues/3611
173175
.. _GH1512: https://github.com/pydata/pandas/issues/1512

pandas/core/indexing.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,8 +778,16 @@ def _has_valid_type(self, key, axis):
778778
def _getitem_tuple(self, tup):
779779

780780
self._has_valid_tuple(tup)
781+
try:
782+
return self._getitem_lowerdim(tup)
783+
except:
784+
pass
785+
781786
retval = self.obj
782787
for i, key in enumerate(tup):
788+
if i >= self.obj.ndim:
789+
raise IndexingError('Too many indexers')
790+
783791
if _is_null_slice(key):
784792
continue
785793

pandas/core/panel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ def _init_arrays(self, arrays, arr_names, axes):
326326

327327
@property
328328
def shape(self):
329-
return [len(getattr(self, a)) for a in self._AXIS_ORDERS]
329+
return tuple([len(getattr(self, a)) for a in self._AXIS_ORDERS])
330330

331331
@classmethod
332332
def from_dict(cls, data, intersect=False, orient='items', dtype=None):

pandas/tests/test_indexing.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,19 @@ def test_set_index_nan(self):
840840
result = df.set_index(['year','PRuid','QC']).reset_index().reindex(columns=df.columns)
841841
assert_frame_equal(result,df)
842842

843+
def test_iloc_panel_issue(self):
844+
845+
# GH 3617
846+
p = Panel(randn(4, 4, 4))
847+
848+
self.assert_(p.iloc[:3, :3, :3].shape == (3,3,3))
849+
self.assert_(p.iloc[1, :3, :3].shape == (3,3))
850+
self.assert_(p.iloc[:3, 1, :3].shape == (3,3))
851+
self.assert_(p.iloc[:3, :3, 1].shape == (3,3))
852+
self.assert_(p.iloc[1, 1, :3].shape == (3,))
853+
self.assert_(p.iloc[1, :3, 1].shape == (3,))
854+
self.assert_(p.iloc[:3, 1, 1].shape == (3,))
855+
843856

844857
if __name__ == '__main__':
845858
import nose

0 commit comments

Comments
 (0)