From 4e5cb4d21cff66145029a9330edf784c41ba4ca1 Mon Sep 17 00:00:00 2001 From: Pietro Battiston Date: Mon, 7 Aug 2017 22:53:30 +0200 Subject: [PATCH] BUG: support pandas objects in iloc with old numpy versions closes #17193 --- doc/source/whatsnew/v0.21.0.txt | 1 + pandas/core/internals.py | 3 +++ pandas/tests/indexing/test_iloc.py | 13 +++++++++++++ 3 files changed, 17 insertions(+) diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index 2f61b71d06019..2c6b1c5bc1897 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -278,6 +278,7 @@ Indexing - Fixes bug where indexing with ``np.inf`` caused an ``OverflowError`` to be raised (:issue:`16957`) - Bug in reindexing on an empty ``CategoricalIndex`` (:issue:`16770`) - Fixes ``DataFrame.loc`` for setting with alignment and tz-aware ``DatetimeIndex`` (:issue:`16889`) +- Avoids ``IndexError`` when passing an Index or Series to ``.iloc`` with older numpy (:issue:`17193`) I/O ^^^ diff --git a/pandas/core/internals.py b/pandas/core/internals.py index 0f85c4e046e5a..7977ef7e7efb9 100644 --- a/pandas/core/internals.py +++ b/pandas/core/internals.py @@ -861,6 +861,9 @@ def _is_empty_indexer(indexer): # set else: + if _np_version_under1p9: + # Work around GH 6168 to support old numpy + indexer = getattr(indexer, 'values', indexer) values[indexer] = value # coerce and try to infer the dtypes of the result diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index 1ba9f3101e7b6..2f74e5cf75064 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -282,6 +282,19 @@ def test_iloc_setitem_list(self): index=["A", "B", "C"], columns=["A", "B", "C"]) tm.assert_frame_equal(df, expected) + def test_iloc_setitem_pandas_object(self): + # GH 17193, affecting old numpy (1.7 and 1.8) + s_orig = Series(range(4)) + expected = Series([0, -1, -2, 3]) + + s = s_orig.copy() + s.iloc[Series([1, 2])] = [-1, -2] + tm.assert_series_equal(s, expected) + + s = s_orig.copy() + s.iloc[pd.Index([1, 2])] = [-1, -2] + tm.assert_series_equal(s, expected) + def test_iloc_setitem_dups(self): # GH 6766