Skip to content

Commit

Permalink
Feature: new generic Apply manipulation in posted.team.
Browse files Browse the repository at this point in the history
This allows applying generic callables to columns.
  • Loading branch information
PhilippVerpoort committed Oct 7, 2024
1 parent 7e67035 commit d213cf4
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion python/posted/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,34 @@ def perform(self, df: pd.DataFrame) -> pd.DataFrame:
return df


# building process chains
# Generic manipulation for applying functions.
class Apply(AbstractManipulation):
_callable: Callable | None
_callables: dict[str, Callable]
def __init__(self,
callable: Callable | None = None,
**kwargs: Callable):
if (callable and kwargs) or (not callable and not kwargs):
ex_msg = ('Please provide either one callable for all columns or '
'specifiy callables for different columns by their names '
'as keyword arguments.')
if callable and kwargs:
ex_msg += ' You cannot provide both at the same time.'
raise Exception(ex_msg)

self._callable = callable
self._callables = kwargs

def perform(self, df: pd.DataFrame) -> pd.DataFrame:
if self._callable:
return df.apply(self._callable)
else:
for col_id, col_callable in self._callables.items():
df = df.assign(col_id=col_callable(df[col_id]))
return df


# Build process chain.
class ProcessChain(AbstractManipulation):
_name: str
_demand: dict[str, dict[str, pint.Quantity]]
Expand Down

0 comments on commit d213cf4

Please sign in to comment.