Skip to content
Snippets Groups Projects
Commit 2a89ec1d authored by David Schäfer's avatar David Schäfer
Browse files

mods to seasonalMask identified in a lengthy discussion

parent 21a4f9b3
No related branches found
No related tags found
3 merge requests!193Release 1.4,!188Release 1.4,!85Seasonal and custom data masking
Pipeline #7066 passed with stage
in 6 minutes and 13 seconds
...@@ -269,35 +269,41 @@ def seasonalMask(dtindex, season_start, season_end, inclusive_selection): ...@@ -269,35 +269,41 @@ def seasonalMask(dtindex, season_start, season_end, inclusive_selection):
When inclusive_selection="season", all above examples work the same way, only that you now When inclusive_selection="season", all above examples work the same way, only that you now
determine wich values NOT TO mask (=wich values are to constitute the "seasons"). determine wich values NOT TO mask (=wich values are to constitute the "seasons").
""" """
if inclusive_selection == "mask": def _replaceBuilder(stamp):
base_bool = False keys = ("second", "minute", "hour", "day", "month", "year")
elif inclusive_selection == "season": stamp_list = map(int, re.split(r"[-T:]", stamp)[::-1])
base_bool = True stamp_kwargs = dict(zip(keys, stamp_list))
else:
raise ValueError("invalid value '{}' was passed to parameter 'inclusive_selection'" def _replace(index):
". Please select from 'mask' and 'season'." if "day" in stamp_kwargs:
.format(inclusive_selection)) stamp_kwargs["day"] = min(stamp_kwargs["day"], index[0].daysinmonth)
out = index[0].replace(**stamp_kwargs)
return out.strftime("%Y-%m-%dT%H:%M:%S")
return _replace
selectors = {"mask": False, "season": True}
if inclusive_selection not in selectors:
raise ValueError(
f"invalid value '{inclusive_selection}' was passed to "
f"parameter 'inclusive_selection'. Please select from "
f"{list(inclusive_selection.keys())}."
)
base_bool = selectors[inclusive_selection]
mask = pd.Series(base_bool, index=dtindex) mask = pd.Series(base_bool, index=dtindex)
def _composeStamp(index, stamp): start_replacer = _replaceBuilder(season_start)
stamp_list = [stamp[i:i + 2] for i in range(len(stamp))][::3] end_replacer = _replaceBuilder(season_end)
index_list = [f"{index.year[0]}", f"{index.month[0]:02}", f"{index.day[0]:02}", f"{index.hour[0]:02}",
f"{index.minute[0]:02}", if pd.Timestamp(start_replacer(dtindex)) <= pd.Timestamp(end_replacer(dtindex)):
f"{index.second[0]:02}"][:-len(stamp_list)] def _selector(x, base_bool=base_bool):
final = index_list + stamp_list x[start_replacer(x.index):end_replacer(x.index)] = not base_bool
# need to account for different month's length:
max_days = (31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
month_day = min(int(final[2]), max_days[int(index.month[0] - 1)])
return f"{final[0]}-{final[1]}-{month_day:02}T{final[3]}:{final[4]}:{final[5]}"
if pd.Timestamp(_composeStamp(dtindex, season_start)) <= pd.Timestamp(_composeStamp(dtindex, season_end)):
def _selector(x, start=season_start, end=season_end, base_bool=base_bool):
x[_composeStamp(x.index, start):_composeStamp(x.index, end)] = not base_bool
return x return x
else: else:
def _selector(x, start=season_start, end=season_end, base_bool=base_bool): def _selector(x, base_bool=base_bool):
x[:_composeStamp(x.index, end)] = not base_bool x[:end_replacer(x.index)] = not base_bool
x[_composeStamp(x.index, start):] = not base_bool x[start_replacer(x.index):] = not base_bool
return x return x
freq = '1' + 'mmmhhhdddMMMYYY'[len(season_start)] freq = '1' + 'mmmhhhdddMMMYYY'[len(season_start)]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment