Home > Software design >  Add first day of the month , last day of the month , nextmontfirstday and next12 days of the month
Add first day of the month , last day of the month , nextmontfirstday and next12 days of the month

Time:12-04

I have a DataFrame that contains MyMonth and I want to use that variable to create other columns like StartDate, EndDate etc. as mentioned below. I have another script that uses these variables as a filter and I don't like to hard code the StartDate, EndDate in my dataset every time I include a new MyMonth. I want to create these variables dynamically using MyMonth

MyMonth           StartDate           EndDate             NextmonthDate       Next12MonthDate
Jan-2018          2018-01-01 00:00:00 2018-01-31 23:59:59 2018-02-01 00:00:00 2019-02-01 00:00:00
Feb-2018          .....
Mar-2018
Apr-2018
May-2018
Jun-2018
Jul-2018
Aug-2018
Sep-2018
Oct-2018
Nov-2018
Dec-2018
Jan-2019
Feb-2019
Mar-2019
Apr-2019
May-2019
Jun-2019
Jul-2019
Aug-2019
Sep-2019
Oct-2019
Nov-2019
Dec-2019
Jan-2020
Feb-2020
Mar-2020
Apr-2020
May-2020

I am currently working on the same using calendar package and looping through my dataset and would really appreciate any help.

import calendar
_, num_days = calendar.monthrange(2018, 1)
first_day = datetime.date(2018, 1, 1)
last_day = datetime.date(2018, 1, num_days)

CodePudding user response:

You can try this :

import calendar
from dateutil.relativedelta import relativedelta

df = pd.DataFrame({'mymonth' : ['Jan-2018', 'Feb-2018', 'Mar-2018']})

StartDate = []
EndDate = []
Next_month_date = []
Next12MonthDate = []

for i in range(len(df)):
    sd = pd.to_datetime(df['mymonth'][i]).replace(day = 1)
    StartDate.append(sd)
    
    next_month = sd   relativedelta(months=1)
    Next_month_date.append(next_month)
    
    next_t_month = sd   relativedelta(months=12)
    Next12MonthDate.append(next_t_month)
    
    ed = pd.to_datetime(df['mymonth'][i])\
        .replace(day = calendar.monthrange(sd.year, sd.month)[-1])
    EndDate.append(ed.replace(hour=23, minute=59, second=59))

dict_ = {"StartDate" : StartDate,
         "EndDate" : EndDate,
         "Next_month_date" : Next_month_date,
         "Next12MonthDate" : Next12MonthDate}
output = pd.concat([df,pd.DataFrame(dict_)],axis=1, join='inner')
  • Related