0

There is a date like this:

Timestamp('2020-10-17 00:00:00')

How can I get the end of next month?

The output should be like this:

Timestamp('2020-11-30 00:00:00')

I tried rrule but it does not work correctly.

My code:

import pandas as pd
from datetime import date
from dateutil.rrule import rrule, MONTHLY

start_date = date(2020, 9, 17)
end_date = date(2020, 10, 31)

for d in rrule(MONTHLY, dtstart=start_date, until=end_date):
    t = pd.Timestamp(d)
    print(t)

The output:

2020-09-17 00:00:00
2020-10-17 00:00:00

I am going to get end of month.

wovano
  • 4,543
  • 5
  • 22
  • 49
Masoud
  • 41
  • 7

7 Answers7

2

in my case, change first day of next 2 month. and then minus 1 day.

i also use this in java.

sample code

import pandas as pd
t = pd.Timestamp('2020-10-17 00:00:00')
t = t.replace(day=1)
answer = t + pd.DateOffset(months=2) + pd.DateOffset(days=-1)
1

In addition to the other's answers, you can also use calender module to get the last day of next month, as follows:

import pandas as pd
from datetime import date
import calendar


def get_nextmonth(d):
    nextmonth_lastday = calendar.monthrange(d.year, d.month+1)[1]  # this return first and last day of the given month, such as (0, 30)
    return date(d.year, d.month+1, nextmonth_lastday)


start_date = date(2020, 9, 17)

print(get_nextmonth(start_date))
# 2020-10-31
Park
  • 2,446
  • 1
  • 16
  • 25
1

Dateutil also works: I set the days to 1, go two months further, and go one day back.

#pip install python-dateutil
from datetime import date
from dateutil.relativedelta import relativedelta
DAY = relativedelta(days=+1)
MONTH = relativedelta(months=+1)
first_date = date(2020, 10, 17).replace(day=1)
print(first_date + 2 * MONTH - DAY)

Output:

2020-11-30
Nineteendo
  • 882
  • 3
  • 18
1

Use pandas vectored way which is fast for large amounts of data.

import pandas as pd

ts = pd.Timestamp(2022, 1, 15)
ts + pd.offsets.DateOffset(months=1) + pd.offsets.MonthEnd(0) --> 

Out: Timestamp('2022-02-28 00:00:00')

ts = pd.Timestamp(2022, 1, 31)
ts + pd.offsets.DateOffset(months=1) + pd.offsets.MonthEnd(0) --> putting 0 to get same month last date though it was last date 

Out: Timestamp('2022-02-28 00:00:00')

ts = pd.Timestamp(2022, 1, 31)
ts + pd.offsets.DateOffset(months=1) + pd.offsets.MonthEnd()

Out: Timestamp('2022-03-31 00:00:00')
Deven Ramani
  • 751
  • 4
  • 10
0

With beautiful-date, you could do:

from beautiful_date import D, months, day, days

last_day_of_the_month = D.today() + 1 * months + 1 * day - 1 * days

This takes the current day (D.today()), goes to the next month (+ 1 * months), goes to the first day of the next month (+ 1 * day), and goes one day back, i.e. last day of the current month (- 1 * days)

And you can easily convert it to datetime:

last_day_of_the_month[0:0]
Yevhen Kuzmovych
  • 10,940
  • 7
  • 28
  • 48
0

You could do it like this:

import pandas as pd
from datetime import datetime, date

date = date(2020, 10, 17)
last_day_of_next_month = pd.Timestamp(datetime.datetime(date.year, ((date.month+2) % 12), 1) - datetime.timedelta(days=1))

Basically, this just created a new date at the first day of the month after next and subtracts one day to get the last day of the next month.

EDIT:

This can be done more cleanly by using pandas DateOffsets as mentioned in the comments and by another answer:

import pandas as pd
from datetime import date

date = date(2020, 10, 17)
last_day_of_next_month = pd.Timestamp(date) + pd.offsets.DateOffset(months=1) + pd.offsets.MonthEnd(0)
Nasser Kessas
  • 359
  • 1
  • 13
0

I would do like this:

from datetime import date,timedelta
start_date = date(2020, 9, 17)
last_day_month=date(start_date.year,start_date.month+1,1)-timedelta(days=1)
Fernando
  • 29
  • 5