pandas: How to work with _iLocIndexer?

后端 未结 1 1430
逝去的感伤
逝去的感伤 2021-01-17 09:38
>>> import pandas as pd
>>> pd.__version__
\'0.16.1\'
>>> s1 = pd.Series([1, 3, 5], index=list(\'abc\'))
>>> s1.iloc(0)


        
相关标签:
1条回答
  • 2021-01-17 10:11

    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.

    0 讨论(0)
提交回复
热议问题