Newest version of Pandas using .iloc method on a data frame threw me IndexError: single positional indexer is out-of-bounds, while no issue with .ix on exactly the same index, albeit .ix being deprecated.

See detailed error below:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-115-d0dc5991a56d> in <module>()
----> 1 user_prod_count.iloc[1684286]

/home/jingw222/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py in __getitem__(self, key)
   1326         else:
   1327             key = com._apply_if_callable(key, self.obj)
-> 1328             return self._getitem_axis(key, axis=0)
   1329 
   1330     def _is_scalar_access(self, key):

/home/jingw222/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py in _getitem_axis(self, key, axis)
   1747 
   1748             # validate the location
-> 1749             self._is_valid_integer(key, axis)
   1750 
   1751             return self._get_loc(key, axis=axis)

/home/jingw222/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py in _is_valid_integer(self, key, axis)
   1636         l = len(ax)
   1637         if key >= l or key < -l:
-> 1638             raise IndexError("single positional indexer is out-of-bounds")
   1639         return True
   1640 

IndexError: single positional indexer is out-of-bounds

Comment From: TomAugspurger

Can you edit your post to have a copy-pastable example showing the error? It seems like your DataFrame isn't that long.

Comment From: jingw222

Just updated the post.

It seems like your DataFrame isn't that long.

What did you mean by that? It works fine with .ix after all.

Comment From: TomAugspurger

user_prod_count is undefined in your example.

Comment From: TomAugspurger

What's len(user_prod_count)?

Comment From: jorisvandenbossche

What did you mean by that? It works fine with .ix after all.

Yes, but ix does not work like iloc. ix will first try label-based indexing, and only fallback to positional if that does not work. iloc is always positional. If it is label-based that you want, you have to switch ix with loc (and this confusion is exactly the reason why we deprecated it)

So the error means that your dataframe is shorter than 1684286 rows.

Comment From: jreback

if you have a repro example pls post.

Comment From: jingw222

@jorisvandenbossche Thank you very much for further clarification. I think we can close this thread now.