Say I have a dataframe with that looks like the following:
usage_price
2017-04-01 00:00:00 1
2017-04-01 00:30:00 1
2017-04-01 01:00:00 1
2017-04-01 01:30:00 1
2017-04-01 02:00:00 1
... ...
2018-12-31 22:00:00 1
2018-12-31 22:30:00 1
2018-12-31 23:00:00 1
2018-12-31 23:30:00 1
What I want to do is update usage_price
for particular fields. In my case, I want to update based on this object:
{'day': '1', 'timerange': ['01 01 00:00', '31 12 08:00']}
Which is:
- Update all Mondays ('day': '1')
- Between the times 00:00 and 08:00
- For any Monday between 01-01 (Jan 1st), and 31-12 (Dec 31st) (ignoring year)
I know how to do some of all these things separately:
df_timeseries[df_timeseries.index.weekday==0, 'usage_price]
df_timeseries.loc[df_timeseries.between_time('00:00', '08:00', include_end=False).index,'usage_price']
But I'm a little stuck on how to get rows between dates (ignoring years), and how to combine everything together - so help would be greatly appreciated!
Edit: This is how far I've managed to get, but I can't seem to get it to work (I'm getting syntax errors), I don't think I'm going about additively building the mask the correct way:
def _create_mask_from_tpr(self, df: pd.DataFrame, tpr: Dict[str, Union[str, List[str]]]) -> Tuple:
print(tpr)
weekday = int(tpr['day']) - 1 # Offset.
start_day, start_month, start_time = tpr['timerange'][0].split(" ")
end_day, end_month, end_time = tpr['timerange'][1].split(" ")
start_year, end_year = df.index.min().year, df.index.max().year
selection_weekday = (df.index.weekday == weekday)
selection_time = (df.between_time(start_time, end_time))
selection_date = None
for year in range(start_year, end_year + 1):
start_date = pd.to_datetime("{}-{}-{}".format(year, start_month, start_day))
end_date = pd.to_datetime("{}-{}-{}".format(year, end_month, end_day))
selection_date = selection_date | (df.index.date() >= start_date & df.index.date() <= end_date)
mask = (selection_weekday & selection_time & selection_date)
print(mask)