How to use the `.loc` method from pandas on a custom class object?












0














I've been going through the source code of pandas https://github.com/pandas-dev/pandas/blob/master/pandas/core/generic.py and I can't figure out where they actually implement the .loc slicing method. I'm working on a wrapper that takes in a bunch of pd.DataFrames. For the sake of this question, let's call it DataFrameCollection. I don't want to inherit all of the methods so I don't want to do class DataFrameCollection(pd.DataFrame): pass.



Does anyone know which code is responsible for the .loc method of a pd.DataFrame object and how this can be used on a custom object?



Essentially I would like to be able to do the following:



dfc_iris =  DataFrameCollection(" a bunch of dataframes")
dfc_iris.loc[idx_obsvs, :]









share|improve this question


















  • 1




    See the _LocIndexer class, and it being added to NDFrame.
    – root
    Nov 12 '18 at 19:39
















0














I've been going through the source code of pandas https://github.com/pandas-dev/pandas/blob/master/pandas/core/generic.py and I can't figure out where they actually implement the .loc slicing method. I'm working on a wrapper that takes in a bunch of pd.DataFrames. For the sake of this question, let's call it DataFrameCollection. I don't want to inherit all of the methods so I don't want to do class DataFrameCollection(pd.DataFrame): pass.



Does anyone know which code is responsible for the .loc method of a pd.DataFrame object and how this can be used on a custom object?



Essentially I would like to be able to do the following:



dfc_iris =  DataFrameCollection(" a bunch of dataframes")
dfc_iris.loc[idx_obsvs, :]









share|improve this question


















  • 1




    See the _LocIndexer class, and it being added to NDFrame.
    – root
    Nov 12 '18 at 19:39














0












0








0







I've been going through the source code of pandas https://github.com/pandas-dev/pandas/blob/master/pandas/core/generic.py and I can't figure out where they actually implement the .loc slicing method. I'm working on a wrapper that takes in a bunch of pd.DataFrames. For the sake of this question, let's call it DataFrameCollection. I don't want to inherit all of the methods so I don't want to do class DataFrameCollection(pd.DataFrame): pass.



Does anyone know which code is responsible for the .loc method of a pd.DataFrame object and how this can be used on a custom object?



Essentially I would like to be able to do the following:



dfc_iris =  DataFrameCollection(" a bunch of dataframes")
dfc_iris.loc[idx_obsvs, :]









share|improve this question













I've been going through the source code of pandas https://github.com/pandas-dev/pandas/blob/master/pandas/core/generic.py and I can't figure out where they actually implement the .loc slicing method. I'm working on a wrapper that takes in a bunch of pd.DataFrames. For the sake of this question, let's call it DataFrameCollection. I don't want to inherit all of the methods so I don't want to do class DataFrameCollection(pd.DataFrame): pass.



Does anyone know which code is responsible for the .loc method of a pd.DataFrame object and how this can be used on a custom object?



Essentially I would like to be able to do the following:



dfc_iris =  DataFrameCollection(" a bunch of dataframes")
dfc_iris.loc[idx_obsvs, :]






python pandas class object indexing






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 19:20









O.rka

7,05529105168




7,05529105168








  • 1




    See the _LocIndexer class, and it being added to NDFrame.
    – root
    Nov 12 '18 at 19:39














  • 1




    See the _LocIndexer class, and it being added to NDFrame.
    – root
    Nov 12 '18 at 19:39








1




1




See the _LocIndexer class, and it being added to NDFrame.
– root
Nov 12 '18 at 19:39




See the _LocIndexer class, and it being added to NDFrame.
– root
Nov 12 '18 at 19:39












1 Answer
1






active

oldest

votes


















9





+25









The loc attribute is one of several indexers, see the pandas.core.indexing module, specifically the get_indexers_list() function:



# the supported indexers
def get_indexers_list():


return [
('ix', _IXIndexer),
('iloc', _iLocIndexer),
('loc', _LocIndexer),
('at', _AtIndexer),
('iat', _iAtIndexer),
]


Each of those classes is defined in the same module.



That function is used to add attributes to the NDFrame class, which is a base class of pandas.DataFrame. Each of the classes in the get_indexers_list() result is added as a property object.



So to re-use the object type, you could add your properties, using the same code if necessary; add the same class method to your class



@classmethod
def _create_indexer(cls, name, indexer):
"""Create an indexer like _name in the class."""
if getattr(cls, name, None) is None:
_indexer = functools.partial(indexer, name)
setattr(cls, name, property(_indexer, doc=indexer.__doc__))


then add the indexers with



# install the indexes
for _name, _indexer in indexing.get_indexers_list():
DataFrameCollection._create_indexer(_name, _indexer)


Given a dfcollection instance of your DataFrameCollection class, dfcollection.loc would then result in _LocIndexer('loc', dfcollection) being called and returned.



Do study the remaining code in pandas.core.indexing to see how each indexer then expects to find information on your DataFrameCollection instance; it's the self.obj reference in the indexer methods.



For example, dfcollection.loc[...] is translated to _LocationIndexer.__getitem__(), which delegates to _LocIndexer._is_scalar_access(), _LocIndexer._getitem_scalar(), _NDFrameIndexer._getitem_tuple() and _LocIndexer._getitem_axis(), which together with methods these delegate to, need access to at least the .axes, .ndim ._get_value(), ._get_axis_name(), ._get_axis_number(), ._get_axis(), ._reindex_with_indexers() and ._take() attributes and methods on the dataframe.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268739%2fhow-to-use-the-loc-method-from-pandas-on-a-custom-class-object%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    9





    +25









    The loc attribute is one of several indexers, see the pandas.core.indexing module, specifically the get_indexers_list() function:



    # the supported indexers
    def get_indexers_list():


    return [
    ('ix', _IXIndexer),
    ('iloc', _iLocIndexer),
    ('loc', _LocIndexer),
    ('at', _AtIndexer),
    ('iat', _iAtIndexer),
    ]


    Each of those classes is defined in the same module.



    That function is used to add attributes to the NDFrame class, which is a base class of pandas.DataFrame. Each of the classes in the get_indexers_list() result is added as a property object.



    So to re-use the object type, you could add your properties, using the same code if necessary; add the same class method to your class



    @classmethod
    def _create_indexer(cls, name, indexer):
    """Create an indexer like _name in the class."""
    if getattr(cls, name, None) is None:
    _indexer = functools.partial(indexer, name)
    setattr(cls, name, property(_indexer, doc=indexer.__doc__))


    then add the indexers with



    # install the indexes
    for _name, _indexer in indexing.get_indexers_list():
    DataFrameCollection._create_indexer(_name, _indexer)


    Given a dfcollection instance of your DataFrameCollection class, dfcollection.loc would then result in _LocIndexer('loc', dfcollection) being called and returned.



    Do study the remaining code in pandas.core.indexing to see how each indexer then expects to find information on your DataFrameCollection instance; it's the self.obj reference in the indexer methods.



    For example, dfcollection.loc[...] is translated to _LocationIndexer.__getitem__(), which delegates to _LocIndexer._is_scalar_access(), _LocIndexer._getitem_scalar(), _NDFrameIndexer._getitem_tuple() and _LocIndexer._getitem_axis(), which together with methods these delegate to, need access to at least the .axes, .ndim ._get_value(), ._get_axis_name(), ._get_axis_number(), ._get_axis(), ._reindex_with_indexers() and ._take() attributes and methods on the dataframe.






    share|improve this answer




























      9





      +25









      The loc attribute is one of several indexers, see the pandas.core.indexing module, specifically the get_indexers_list() function:



      # the supported indexers
      def get_indexers_list():


      return [
      ('ix', _IXIndexer),
      ('iloc', _iLocIndexer),
      ('loc', _LocIndexer),
      ('at', _AtIndexer),
      ('iat', _iAtIndexer),
      ]


      Each of those classes is defined in the same module.



      That function is used to add attributes to the NDFrame class, which is a base class of pandas.DataFrame. Each of the classes in the get_indexers_list() result is added as a property object.



      So to re-use the object type, you could add your properties, using the same code if necessary; add the same class method to your class



      @classmethod
      def _create_indexer(cls, name, indexer):
      """Create an indexer like _name in the class."""
      if getattr(cls, name, None) is None:
      _indexer = functools.partial(indexer, name)
      setattr(cls, name, property(_indexer, doc=indexer.__doc__))


      then add the indexers with



      # install the indexes
      for _name, _indexer in indexing.get_indexers_list():
      DataFrameCollection._create_indexer(_name, _indexer)


      Given a dfcollection instance of your DataFrameCollection class, dfcollection.loc would then result in _LocIndexer('loc', dfcollection) being called and returned.



      Do study the remaining code in pandas.core.indexing to see how each indexer then expects to find information on your DataFrameCollection instance; it's the self.obj reference in the indexer methods.



      For example, dfcollection.loc[...] is translated to _LocationIndexer.__getitem__(), which delegates to _LocIndexer._is_scalar_access(), _LocIndexer._getitem_scalar(), _NDFrameIndexer._getitem_tuple() and _LocIndexer._getitem_axis(), which together with methods these delegate to, need access to at least the .axes, .ndim ._get_value(), ._get_axis_name(), ._get_axis_number(), ._get_axis(), ._reindex_with_indexers() and ._take() attributes and methods on the dataframe.






      share|improve this answer


























        9





        +25







        9





        +25



        9




        +25




        The loc attribute is one of several indexers, see the pandas.core.indexing module, specifically the get_indexers_list() function:



        # the supported indexers
        def get_indexers_list():


        return [
        ('ix', _IXIndexer),
        ('iloc', _iLocIndexer),
        ('loc', _LocIndexer),
        ('at', _AtIndexer),
        ('iat', _iAtIndexer),
        ]


        Each of those classes is defined in the same module.



        That function is used to add attributes to the NDFrame class, which is a base class of pandas.DataFrame. Each of the classes in the get_indexers_list() result is added as a property object.



        So to re-use the object type, you could add your properties, using the same code if necessary; add the same class method to your class



        @classmethod
        def _create_indexer(cls, name, indexer):
        """Create an indexer like _name in the class."""
        if getattr(cls, name, None) is None:
        _indexer = functools.partial(indexer, name)
        setattr(cls, name, property(_indexer, doc=indexer.__doc__))


        then add the indexers with



        # install the indexes
        for _name, _indexer in indexing.get_indexers_list():
        DataFrameCollection._create_indexer(_name, _indexer)


        Given a dfcollection instance of your DataFrameCollection class, dfcollection.loc would then result in _LocIndexer('loc', dfcollection) being called and returned.



        Do study the remaining code in pandas.core.indexing to see how each indexer then expects to find information on your DataFrameCollection instance; it's the self.obj reference in the indexer methods.



        For example, dfcollection.loc[...] is translated to _LocationIndexer.__getitem__(), which delegates to _LocIndexer._is_scalar_access(), _LocIndexer._getitem_scalar(), _NDFrameIndexer._getitem_tuple() and _LocIndexer._getitem_axis(), which together with methods these delegate to, need access to at least the .axes, .ndim ._get_value(), ._get_axis_name(), ._get_axis_number(), ._get_axis(), ._reindex_with_indexers() and ._take() attributes and methods on the dataframe.






        share|improve this answer














        The loc attribute is one of several indexers, see the pandas.core.indexing module, specifically the get_indexers_list() function:



        # the supported indexers
        def get_indexers_list():


        return [
        ('ix', _IXIndexer),
        ('iloc', _iLocIndexer),
        ('loc', _LocIndexer),
        ('at', _AtIndexer),
        ('iat', _iAtIndexer),
        ]


        Each of those classes is defined in the same module.



        That function is used to add attributes to the NDFrame class, which is a base class of pandas.DataFrame. Each of the classes in the get_indexers_list() result is added as a property object.



        So to re-use the object type, you could add your properties, using the same code if necessary; add the same class method to your class



        @classmethod
        def _create_indexer(cls, name, indexer):
        """Create an indexer like _name in the class."""
        if getattr(cls, name, None) is None:
        _indexer = functools.partial(indexer, name)
        setattr(cls, name, property(_indexer, doc=indexer.__doc__))


        then add the indexers with



        # install the indexes
        for _name, _indexer in indexing.get_indexers_list():
        DataFrameCollection._create_indexer(_name, _indexer)


        Given a dfcollection instance of your DataFrameCollection class, dfcollection.loc would then result in _LocIndexer('loc', dfcollection) being called and returned.



        Do study the remaining code in pandas.core.indexing to see how each indexer then expects to find information on your DataFrameCollection instance; it's the self.obj reference in the indexer methods.



        For example, dfcollection.loc[...] is translated to _LocationIndexer.__getitem__(), which delegates to _LocIndexer._is_scalar_access(), _LocIndexer._getitem_scalar(), _NDFrameIndexer._getitem_tuple() and _LocIndexer._getitem_axis(), which together with methods these delegate to, need access to at least the .axes, .ndim ._get_value(), ._get_axis_name(), ._get_axis_number(), ._get_axis(), ._reindex_with_indexers() and ._take() attributes and methods on the dataframe.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 17 '18 at 12:40

























        answered Nov 17 '18 at 12:12









        Martijn Pieters

        701k13224302267




        701k13224302267






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268739%2fhow-to-use-the-loc-method-from-pandas-on-a-custom-class-object%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini