Skip to content

Commit

Permalink
Update mask_values to return a series
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonZogk committed May 17, 2024
1 parent 3509145 commit a3067c1
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/forward_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

import numpy as np
import pandas as pd
from pandas.core.base import PandasObject


def mask_values(
df: pd.DataFrame, target_variable: List[str] or str, expr: str
) -> pd.DataFrame:
def mask_values(df: pd.DataFrame, target_variable: str, expr: str) -> pd.Series:
"""Convert values in a dataframe column to 0 based on a python expression
Parameters
Expand All @@ -22,13 +19,14 @@ def mask_values(
Returns
-------
df : pd.Dataframe
df : pd.Series
"""
masked_column = df[target_variable].copy()

try:
df.loc[~(df.eval(expr)), target_variable] = 0
masked_column.loc[~(df.eval(expr))] = np.nan

except ValueError:
print(
Expand All @@ -38,8 +36,7 @@ def mask_values(
"""
)


PandasObject.mask_values = mask_values
return masked_column


def calculate_imputation_link(
Expand Down

0 comments on commit a3067c1

Please sign in to comment.