Reputation: 6128
>>> 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?
Upvotes: 19
Views: 13797
Reputation: 6128
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.
Upvotes: 29