From 3d90ad4ba7bc2c44e21bd23d325625d219db8361 Mon Sep 17 00:00:00 2001 From: jreback Date: Sun, 6 Oct 2013 19:53:28 -0400 Subject: [PATCH] BUG: Treat a list/ndarray identically for iloc indexing with list-like (GH5006) --- doc/source/release.rst | 3 +-- pandas/core/indexing.py | 4 +++- pandas/tests/test_indexing.py | 7 +++++++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/doc/source/release.rst b/doc/source/release.rst index 8488d03f97cbd..3e072da164ab2 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -297,7 +297,6 @@ API Changes (:issue:`4501`) - Support non-unique axes in a Panel via indexing operations (:issue:`4960`) - Internal Refactoring ~~~~~~~~~~~~~~~~~~~~ @@ -566,7 +565,7 @@ Bug Fixes - Provide automatic conversion of ``object`` dtypes on fillna, related (:issue:`5103`) - Fixed a bug where default options were being overwritten in the option parser cleaning (:issue:`5121`). - + - Treat a list/ndarray identically for ``iloc`` indexing with list-like (:issue:`5006`) pandas 0.12.0 ------------- diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 7502b3898d7fb..69114166b3406 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -1092,7 +1092,9 @@ def _getitem_axis(self, key, axis=0): else: if _is_list_like(key): - pass + + # force an actual list + key = list(key) else: key = self._convert_scalar_indexer(key, axis) diff --git a/pandas/tests/test_indexing.py b/pandas/tests/test_indexing.py index 67c87277647c8..6292c5874772f 100644 --- a/pandas/tests/test_indexing.py +++ b/pandas/tests/test_indexing.py @@ -333,8 +333,15 @@ def test_iloc_getitem_list_int(self): # list of ints self.check_result('list int', 'iloc', [0,1,2], 'ix', { 0 : [0,2,4], 1 : [0,3,6], 2: [0,4,8] }, typs = ['ints']) + self.check_result('list int', 'iloc', [2], 'ix', { 0 : [4], 1 : [6], 2: [8] }, typs = ['ints']) self.check_result('list int', 'iloc', [0,1,2], 'indexer', [0,1,2], typs = ['labels','mixed','ts','floats','empty'], fails = IndexError) + # array of ints + # (GH5006), make sure that a single indexer is returning the correct type + self.check_result('array int', 'iloc', np.array([0,1,2]), 'ix', { 0 : [0,2,4], 1 : [0,3,6], 2: [0,4,8] }, typs = ['ints']) + self.check_result('array int', 'iloc', np.array([2]), 'ix', { 0 : [4], 1 : [6], 2: [8] }, typs = ['ints']) + self.check_result('array int', 'iloc', np.array([0,1,2]), 'indexer', [0,1,2], typs = ['labels','mixed','ts','floats','empty'], fails = IndexError) + def test_iloc_getitem_dups(self): # no dups in panel (bug?)