How to check if a date is the last day of a given month in pandas
Overview
The is_month_end attribute in pandas.Timestamp (an equivalent of Python's DateTime object) is used to check if the given date is the last day of the month.
Syntax
Timestamp.is_month_end
Parameters
This attribute takes no parameter value.
Return value
This attribute returns a Boolean value. It returns True to indicate that the date is the last day of the month. Otherwise, it returns False.
Example
# A code to illustrate the .is_month_end attribute in Pandas# importing the pandas libraryimport pandas as pd# creating Timestamp objectsa = pd.Timestamp(2022, 4, 19)b = pd.Timestamp(2022, 4, 30)# checking if the dates is or are the last day of the month of Aprilprint(a.is_month_end)print(b.is_month_end)
Explanation
- Line 3: We import the
pandaslibrary. - Line 6 and 7: We create time stamp objects,
aandb. - Line 10 and 11: We obtain and print to console, the result indicating which of
aandbis the last day of the month using theis_month_endattribute.