Skip to content

Commit 325afdf

Browse files
committed
BUG: fix regression in negative integer indexing from 0.7.x close #1888
1 parent 68251c2 commit 325afdf

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

RELEASE.rst

+7
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ pandas 0.9.0
4848
- Add quoting option for DataFrame.to_csv (#1902)
4949
- Indicate long column value truncation in DataFrame output with ... (#1854)
5050
- DataFrame.dot will not do data alignment, and also work with Series (#1915)
51+
- Add ``na`` option for missing data handling in some vectorized string
52+
methods (#1689)
5153

5254
**API Changes**
5355

@@ -169,6 +171,11 @@ pandas 0.9.0
169171
- Raise Exception if set passed to Series constructor (#1913)
170172
- Add TypeError when appending HDFStore table w/ wrong index type (#1881)
171173
- Don't raise exception on empty inputs in EW functions (e.g. ewma) (#1900)
174+
- Make asof work correctly with PeriodIndex (#1883)
175+
- Fix extlinks in doc build
176+
- Fill boolean DataFrame with NaN when calling shift (#1814)
177+
- Fix setuptools bug causing pip not to Cythonize .pyx files sometimes
178+
- Fix negative integer indexing regression in .ix from 0.7.x (#1888)
172179

173180
pandas 0.8.1
174181
============

pandas/core/indexing.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -293,21 +293,27 @@ def _reindex(keys, level=None):
293293
inds, = np.asarray(key, dtype=bool).nonzero()
294294
return self.obj.take(inds, axis=axis)
295295
else:
296-
if isinstance(key, Index):
296+
was_index = isinstance(key, Index)
297+
if was_index:
297298
# want Index objects to pass through untouched
298299
keyarr = key
299300
else:
300301
# asarray can be unsafe, NumPy strings are weird
301302
keyarr = _asarray_tuplesafe(key)
302303

303304
if _is_integer_dtype(keyarr):
305+
if labels.inferred_type != 'integer':
306+
keyarr = np.where(keyarr < 0,
307+
len(labels) + keyarr, keyarr)
308+
304309
if labels.inferred_type == 'mixed-integer':
305310
indexer = labels.get_indexer(keyarr)
306311
if (indexer >= 0).all():
307312
self.obj.take(indexer, axis=axis)
308313
else:
309314
return self.obj.take(keyarr, axis=axis)
310315
elif not labels.inferred_type == 'integer':
316+
311317
return self.obj.take(keyarr, axis=axis)
312318

313319
# this is not the most robust, but...
@@ -406,6 +412,9 @@ def _convert_to_indexer(self, obj, axis=0):
406412

407413
# If have integer labels, defer to label-based indexing
408414
if _is_integer_dtype(objarr) and not is_int_index:
415+
if labels.inferred_type != 'integer':
416+
objarr = np.where(objarr < 0,
417+
len(labels) + objarr, objarr)
409418
return objarr
410419

411420
# this is not the most robust, but...

pandas/tests/test_frame.py

+16
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,22 @@ def test_getitem_ix_mixed_integer(self):
175175
expected = df.ix[Index([1, 10], dtype=object)]
176176
assert_frame_equal(result, expected)
177177

178+
def test_getitem_setitem_ix_negative_integers(self):
179+
result = self.frame.ix[:, -1]
180+
assert_series_equal(result, self.frame['D'])
181+
182+
result = self.frame.ix[:, [-1]]
183+
assert_frame_equal(result, self.frame[['D']])
184+
185+
result = self.frame.ix[:, [-1, -2]]
186+
assert_frame_equal(result, self.frame[['D', 'C']])
187+
188+
self.frame.ix[:, [-1]] = 0
189+
self.assert_((self.frame['D'] == 0).all())
190+
191+
df = DataFrame(np.random.randn(8, 4))
192+
self.assert_(isnull(df.ix[:, [-1]].values).all())
193+
178194
def test_getattr(self):
179195
tm.assert_series_equal(self.frame.A, self.frame['A'])
180196
self.assertRaises(AttributeError, getattr, self.frame,

0 commit comments

Comments
 (0)