Search code examples
python-3.xpandasindexinggroup-byoperator-overloading

How is the square bracket operator implemented for pandas GroupBy objects?


In Python 3.11: to apply the square brackets operator to an object, the object's __getitem__ method should be implemented.

In Python's pandas module it is possible to apply square brackets to objects returned from a DataFrame by the groupby() method, e.g. tips.groupby("sex")["total_bill"].count(). (Example taken from this tutorial.)

However, there's no __getitem__ method listed in the pandas' API reference for GroupBy objects. How come? How is the square bracket operator implemented for pandas GroupBy objects?


Solution

  • Sometimes I use code like below to see the source if available:

    import inspect
    import pandas
    
    groupby_obj = pandas.DataFrame().groupby(level=0)
    print(f'{type(groupby_obj)=}\n{"-"*80}')
    
    assert '__getitem__' in dir(groupby_obj)
    code = inspect.getsource(groupby_obj.__getitem__)
    
    print(code)
    

    Or rather, I just type ?? after the functions to see the code in Jupyter Notebook. Anyway, you can definitely see __getitem__ in GroupBy object and read its source code.

    As of mentioning __getitem__ in the pandas' API reference for GroupBy, I suppose that this part about selection in GroupBy was intended to explain how selection works in this case.