Coder
Coder

Reputation: 37

How to print remaining months from current month of a year in python pandas?

This for loop is created to print the previous months by passing the range, but I want to display future months from the current month for example month = 6 then it should print remaining months of the year

year = 2021
month = 6
for i in range(1,month):
    thdate = datetime(year,i,calendar.monthrange(year, i)[1])
thdate

Upvotes: 0

Views: 95

Answers (2)

David
David

Reputation: 8298

Another different solution is to use monthdelta package.

Something like a do-while loop or you could do it in other ways (using a regular for loop):

from datetime import date
import monthdelta as md

curr_date = date(2021, 6, 1)

y = curr_date.year
first_iter = True
while first_iter or y == curr_date.year:
    print(curr_date.month)
    curr_date = curr_date + md.monthdelta(1)
    first_iter = False

Upvotes: 0

Ynjxsjmh
Ynjxsjmh

Reputation: 30002

IIUC, you can try

year = 2021
month = 6
for i in range(month, 13):  # <--- Modify the `range` to [month, 13)
    thdate = datetime(year,i,calendar.monthrange(year, i)[1])

Upvotes: 1

Related Questions