Pandas to_period: Comparing dates by month, quarter, or year

Pandas to_period: Comparing dates by month, quarter, or year

Want to check if two Python Pandas datetimes are in the same month/quarter/year?

Convert them both to periods, and compare with ==:

date1.to_period('M') == date2.to_period('M')  # same month?
date1.to_period('Q') == date2.to_period('Q')  # same quarter?