Last Day of Month - DataFrame

Viewed 318

Good evening, I have the following data in Pandas DataFrame:

Date DT_INI
2021-03-01 01032021
2021-02-06 06022021

I wish to convert any of these 2 columns to get the last day of its data month.

enter image description here

Already tried lots of function.. but still nothing.

1 Answers

You can use the pd.offset.MonthEnd(n=0) as offset to add on the base dates to get the month-end dates, as follows:

df['Date_Month_End'] = df['Date'] + pd.offsets.MonthEnd(n=0)


print(df)

        Date   DT_INI Date_Month_End
0 2021-03-01  1032021     2021-03-31
1 2021-02-06  6022021     2021-02-28

Parameter n=0 is specified so that Date of 2021-03-31 will not roll forward to 2021-04-30

Related