Home > Software design >  How to print remaining months from current month of a year in python pandas?
How to print remaining months from current month of a year in python pandas?

Time:06-23

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

CodePudding user response:

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])

CodePudding user response:

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
  • Related