# To subtract or add months from date you can use the third party dateutil module.
import datetime
from dateutil.relativedelta import relativedelta
date = datetime.datetime.strptime("2021-12-31", "%Y-%m-%d")
date2 = date - relativedelta(months=1)
print date2