BUG: Series.iloc[[-1]] error when Series is of length 1 · Issue #10547 · pandas-dev/pandas (original) (raw)

When a Series is of length one, I would expect iloc[[-1]] to produce the same result as iloc[[0]]. However iloc[[-1]] produces an error, even though iloc[[0]] and iloc[-1] work.

In [178]: import pandas as pd

In [179]: pd.Series(['a', 'b'], index=['A', 'B']).iloc[[-1]]
Out[179]:
B    b
dtype: object

In [180]: pd.Series(['a'], index=['A']).iloc[[-1]]  # BUG: This should return the same as [181].
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-180-47228385ee31> in <module>()
----> 1 pd.Series(['a'], index=['A']).iloc[[-1]]

C:\Python34\lib\site-packages\pandas\core\indexing.py in __getitem__(self, key)
   1187             return self._getitem_tuple(key)
   1188         else:
-> 1189             return self._getitem_axis(key, axis=0)
   1190
   1191     def _getitem_axis(self, key, axis=0):

C:\Python34\lib\site-packages\pandas\core\indexing.py in _getitem_axis(self, key, axis)
   1463
   1464                 # validate list bounds
-> 1465                 self._is_valid_list_like(key, axis)
   1466
   1467                 # force an actual list

C:\Python34\lib\site-packages\pandas\core\indexing.py in _is_valid_list_like(self, key, axis)
   1402         l = len(ax)
   1403         if len(arr) and (arr.max() >= l or arr.min() <= -l):
-> 1404             raise IndexError("positional indexers are out-of-bounds")
   1405
   1406         return True

IndexError: positional indexers are out-of-bounds

In [181]: pd.Series(['a'], index=['A']).iloc[[0]]
Out[181]:
A    a
dtype: object

In [182]: pd.Series(['a'], index=['A']).iloc[-1]
Out[182]: 'a'

In [183]: pd.__version__
Out[183]: '0.16.2'