Skip to content

BUG: Series.iloc[[-1]] error when Series is of length 1 #10547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
seth-p opened this issue Jul 11, 2015 · 1 comment · Fixed by #10808
Closed

BUG: Series.iloc[[-1]] error when Series is of length 1 #10547

seth-p opened this issue Jul 11, 2015 · 1 comment · Fixed by #10808
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@seth-p
Copy link
Contributor

seth-p commented Jul 11, 2015

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'
@jreback
Copy link
Contributor

jreback commented Jul 12, 2015

I think the test should be arr.min() < -l (and not <=). pull-request to fix?

@jreback jreback added Bug Indexing Related to indexing on series/frames, not to indexes themselves labels Jul 12, 2015
@jreback jreback added this to the 0.17.0 milestone Jul 12, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants