Given a '%Y-%m'
(e.g. 2022-03
), what's a good way to get a list of the last days of its previous N
(e.g. 5
) months with desired results:
['2021-10-31', '2021-11-30', '2021-12-31', '2022-01-31', '2022-02-28']
Given a '%Y-%m'
(e.g. 2022-03
), what's a good way to get a list of the last days of its previous N
(e.g. 5
) months with desired results:
['2021-10-31', '2021-11-30', '2021-12-31', '2022-01-31', '2022-02-28']
As mentioned in comments, you can use a list comprehension to generate a list of previous month starts, then subtract one day from each of them.
def previous_month_ends(date, months):
year, month, day = [int(x) for x in date.split('-')]
d = datetime.date(year, month, day)
t = datetime.timedelta(1)
s = datetime.date(year, month, 1)
return [(x - t).strftime('%Y-%m-%d')
for m in range(months - 1, -1, -1)
for x in (datetime.date(s.year, s.month - m, s.day) if s.month > m else \
datetime.date(s.year - 1, s.month - (m - 12), s.day),)]
You'll want to do some math to ensure you handle the previous dates going into the previous year(s). The above will need to be modified to handle lartge values for months
.
I figured Pandas
already has Month End (M)
freq in its builtin date_range
function:
>>> end = datetime.strptime('2022-03', '%Y-%m')
>>> start = end - pd.DateOffset(months=5)
>>> pd.date_range(start, end, freq='M')
DatetimeIndex(['2021-10-31', '2021-11-30', '2021-12-31', '2022-01-31', '2022-02-28'], dtype='datetime64[ns]', freq=None)