New suggestion
As long as you make sure the function you pass requires a dataframe as its first argument, the problem becomes simple as (as already noted by @jjramsey
):
class our_class():
def __init__(self, df):
self.df = df
def arb_func(self, func):
return func(self.df)
Virtually any method of pd.DataFrame
, i.e. a method having a self
as first input, for instance pd.DataFrame.max
source, is directly compatible with this use. In this version you would have to be passing partial
functions every time you would need some additional configurations in the form of arguments and keyword arguments. In your case this is the use of axis=1
. A little modification to the above implementation can account for such situations:
class our_class():
def __init__(self, df):
self.df = df
def arb_func(self, func, *args, **kwargs):
return func(self.df, *args, **kwargs)
Now this implementation is that generic that you can pass your own functions as well as long as the first parameter is the dataframe. For instance, you would like to count how many apples you have with your own count_apples
function as:
def count_apples(df, apples_column_name):
return df[apples_column_name].eq('apple').sum()
Now making use of it as:
df = pd.DataFrame({"fruits_in_store": ["apple", "apple", "pear", "banana", "papaya"]})
ob1.arb_func(count_apples, "fruits_in_store") # it is possible to pass this into the `apples_column_name` as an arg
ob1.arb_func(count_apples, apples_column_name="fruits_in_store") # or you can be explicit
Original answer
I assume the OP is trying to generate some generic coding interface for educational purposes?
Here a suggestion (which in my opinion is actually making the usage way more complex than necessary, as many other users have already noted in their questions/comments):
from functools import partial
import pandas as pd
df = pd.DataFrame({"a": [1, 2, 3]})
class our_class():
def __init__(self, df):
self.df = df
def arb_func(self, func: str, **kwargs):
return partial(getattr(pd.DataFrame, func), **kwargs)(df)
ob1 = our_class(df)
print(ob1.arb_func("max", axis=1))
0 1
1 2
2 3
dtype: int64
print(ob1.arb_func("max", axis=0))
a 3
dtype: int64