masking
Mask
Class to define masks with conditions and weights to apply to DataFiles
Parameters:
Name | Type | Description | Default |
---|---|---|---|
where |
MaskCondition | list[MaskCondition]
|
Where the mask should be applied |
None
|
use |
MaskCondition | list[MaskCondition]
|
Condition on where to use the masks |
None
|
weight |
None | float | str | list[float | str]
|
Weights to apply |
None
|
other |
float
|
|
nan
|
comment |
str
|
|
''
|
Source code in python/posted/masking.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|
__init__(where=None, use=None, weight=None, other=np.nan, comment='')
set fields from constructor arguments, perform consistency checks on fields, set default weight to 1 if not set otherwise
Source code in python/posted/masking.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
|
get_weights(df)
Apply weights to the dataframe
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
Dataframe to apply weights on |
required |
Returns:
Type | Description |
---|---|
pd.DataFrame
|
Dataframe with applied weights |
Source code in python/posted/masking.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|
matches(df)
Check if a mask matches a dataframe (all 'where' conditions match across all rows)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
Dataframe to check for matches |
required |
Returns:
Type | Description |
---|---|
bool
|
If the mask matches the dataframe |
Source code in python/posted/masking.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
|
apply_cond(df, cond)
Takes a pandas DataFrame and a condition, which can be a string, dictionary,
or callable, and applies the condition to the DataFrame using eval
or apply
accordingly.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df |
DataFrame
|
A pandas DataFrame containing the data on which the condition will be applied. |
required |
cond |
MaskCondition
|
The condition to be applied on the dataframe. Can be either a string, a dictionary, or a callable function. |
required |
Returns:
Type | Description |
---|---|
pd.DataFrame
|
Dataframe evaluated at the mask condition |
Source code in python/posted/masking.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
read_masks(variable)
Reads YAML files containing mask specifications from multiple databases and returns a list of Mask objects.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
variable |
str
|
Variable to be read |
required |
Returns:
Type | Description |
---|---|
list
|
List with masks for the variable |
Source code in python/posted/masking.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
|