I have a time series dataframe at the monthly level where each month is denoted as the last day of the month. I need to loop through the dataframe one step at a time to get forecast from different time periods.
I tried the following:
import pandas as pd
from pandas.tseries.offsets import DateOffset
times = np.arange(1,24)
for time in times:
date_min = pd.to_datetime("2020-11-30")
offset_ = int(time)
filter_ = date_min DateOffset(months = int)
df_ = df[(df['dmand_yr_mo'] <= filter_)]
This runs error-free. However, when going from November to December, the date ends up being 2020-12-30 instead of 2020-12-31. Using this method cuts off the day.
Is there a better way to achieve what I want without cutting off days?
CodePudding user response:
Instead of DateOffset
, you can use MonthEnd
:
>>> date_min pd.offsets.MonthEnd(1)
Timestamp('2020-12-31 00:00:00')