I was very tempted to inherit and subclass pandas DataFramefor my use case, but from all that I read about good programming and Pandas documentation on internal documents is the wrong thing.
As a simplified example, I can say that my lines DataFrameare people, and the fields are height and weight. I am prone to subclassing so I have a method bmi(). I would do it as follows:
import pandas as pd
class People(pd.DataFrame):
def __init__(self, *args, **kwargs):
super(People, self).__init__(*args, **kwargs)
def bmi(self):
return self['weight'] / self['height'] ** 2 x 703
people = People([[172, 74], [100, 60]])
bmi = people.bmi()
How can I do this using composition? It would be something like:
class People(object):
def __init__(self, data):
self.data = pd.DataFrame(data)
def bmi(self):
return self.data['weight'] / self.data['height'] ** 2 x 703
people = People([[172, 74], [100, 60]])
bmi = people.bmi()
If not, how do I do this? If so, why is it better?
Note. I understand that this is a simplified example.