When subclassing a DataFrame, how to write a wrapper method for 'loc'

I am trying to write a wrapper method for a subclass DataFrame

class SubDataFrame(DataFrame):
   ...

a = SubDataFrame()
b = a.loc[row, column]

on the last line, I want to process the metadata separately in the class SubDataFramebefore or after processing locin the superclass.

I do not know how to implement this notation, because if I just define

def loc(self, *args, **kwargs)

The parenthesis SubDataFramenotation loc[]does not work. ( AttributeError: instancemethod has no attribute getitem).

I can not find documents on how to implement this.

+4
source share
1 answer

Unfortunately, the way Pandas indexes is far from simple.

, (, obj[something]), __getitem__ __setitem__ (. ).

DataFrame loc - , , :

>>> frame = pd.DataFrame()
>>> type(frame.loc)
pandas.core.indexing._LocIndexer

. _LocIndexer ( ) _NDFrameIndexer, __getitem__ __setitem__ .

.loc[] , , , -, , . , .

+4

Source: https://habr.com/ru/post/1623960/


All Articles