pandas: How to work with _iLocIndexer?

匿名 (未验证) 提交于 2019-12-03 00:46:02

问题:

>>> 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?

回答1:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!