0

Haven't found anything addressing this in a pandas dataframe.

So far I've tried this based on another stack overflow post but it is not working:

df['last_day_in_month'] = pd.Period(pd.to_datetime(df['date']),freq='M').end_time.date()

Pls excuse the quick picture but basically trying to replicate this in a pandas dataframe: enter image description here

Katsu
  • 8,479
  • 3
  • 15
  • 16
  • Does this answer your question? [Find the end of the month of a Pandas DataFrame Series](https://stackoverflow.com/questions/37354105/find-the-end-of-the-month-of-a-pandas-dataframe-series) – ouroboros1 Jun 14 '22 at 23:42

2 Answers2

1

Try this:

df['last_day_in_month'] = df['Date'] + pd.tseries.offsets.MonthEnd(0)
rhug123
  • 7,893
  • 1
  • 9
  • 24
0

You can use pandas.Period with the corresponding month frequency to get the month, then catch the last day with the "end_time" attribute:

df['date'].apply(lambda d: pd.Period(d, freq='M').end_time.date()) 

Then you can assign this value to your new column.

lemon
  • 14,875
  • 6
  • 18
  • 38