Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: new generic Apply manipulation in posted.team. #27

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading