Skip to content

Open In Colab

Pandas TimeSeries MonthBegin and MonthEnd

import pandas as pd
from pandas.tseries.offsets import MonthBegin, MonthEnd

MonthEnd

Helpful when figuring out whether month ends on 28th, 29th, 30th, 31st.

todays_date = '2021-02-07'
month_end_date = pd.to_datetime(todays_date) + MonthEnd(1)
print(month_end_date)
2021-02-28 00:00:00

MonthBegin

Helpful when figuring out when current/previous/next MonthBegin.

month_start_date = pd.to_datetime(todays_date) + MonthBegin(-1)
print(month_start_date)
2021-02-01 00:00:00

next_month_start_date = pd.to_datetime(todays_date) + MonthBegin(1)
print(next_month_start_date)
2021-03-01 00:00:00