Skip to content

Commit 5d8319e

Browse files
toobazjreback
authored andcommitted
BUG: support pandas objects in iloc with old numpy versions (#17194)
closes #17193
1 parent e5aad1a commit 5d8319e

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

doc/source/whatsnew/v0.21.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ Indexing
322322
- Fixes bug where indexing with ``np.inf`` caused an ``OverflowError`` to be raised (:issue:`16957`)
323323
- Bug in reindexing on an empty ``CategoricalIndex`` (:issue:`16770`)
324324
- Fixes ``DataFrame.loc`` for setting with alignment and tz-aware ``DatetimeIndex`` (:issue:`16889`)
325+
- Avoids ``IndexError`` when passing an Index or Series to ``.iloc`` with older numpy (:issue:`17193`)
325326

326327
I/O
327328
^^^

pandas/core/internals.py

+3
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,9 @@ def _is_empty_indexer(indexer):
857857

858858
# set
859859
else:
860+
if _np_version_under1p9:
861+
# Work around GH 6168 to support old numpy
862+
indexer = getattr(indexer, 'values', indexer)
860863
values[indexer] = value
861864

862865
# coerce and try to infer the dtypes of the result

pandas/tests/indexing/test_iloc.py

+13
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,19 @@ def test_iloc_setitem_list(self):
282282
index=["A", "B", "C"], columns=["A", "B", "C"])
283283
tm.assert_frame_equal(df, expected)
284284

285+
def test_iloc_setitem_pandas_object(self):
286+
# GH 17193, affecting old numpy (1.7 and 1.8)
287+
s_orig = Series([0, 1, 2, 3])
288+
expected = Series([0, -1, -2, 3])
289+
290+
s = s_orig.copy()
291+
s.iloc[Series([1, 2])] = [-1, -2]
292+
tm.assert_series_equal(s, expected)
293+
294+
s = s_orig.copy()
295+
s.iloc[pd.Index([1, 2])] = [-1, -2]
296+
tm.assert_series_equal(s, expected)
297+
285298
def test_iloc_setitem_dups(self):
286299

287300
# GH 6766

0 commit comments

Comments
 (0)