Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas: How to work with _iLocIndexer?

>>> import pandas as pd
>>> pd.__version__
'0.16.1'
>>> s1 = pd.Series([1, 3, 5], index=list('abc'))
>>> s1.iloc(0)
<pandas.core.indexing._iLocIndexer object at 0x10ca3f690>

I expected the integer 1 to be returned for s1.iloc(0), but got an _iLocIndexer object instead. What can I do with this object? How do I work with it?

like image 608
user650654 Avatar asked May 26 '15 00:05

user650654


1 Answers

The solution, due to @JohnE, is to use square brackets [0] instead of (0) with iloc.

Some more info after digging through some pandas code.

s1.iloc makes iloc look like a method. But it is an attribute and the value of the attribute is _iLocIndexer. _iLocIndexer is callable with arguments and it returns a copy of itself ignoring any args or kwargs (see pandas.core.indexing._NDFrameIndexer.call in pandas code). This is why s1.iloc(0) is a valid call.

When s1.iloc[0] is called with square brackets, an _iLocIndexer is instantiated and the call to [] operator results in a call to the iLocIndexer's `getitem' method.

The same is true for the other indexers - loc, ix, at and iat. These indexers are installed using setattr in class method pandas.core.generic.NDFrame._create_indexer.

like image 200
user650654 Avatar answered Sep 17 '22 17:09

user650654