0

I have a code asks the user to input a date:

date_entry = input('Inserisci la data di fine mese del lancio del MAI in formato DD-MM-YYYY: ')
year, month, day = map(int, date_entry.split('-'))
date1 = pd.to_datetime(pd.Series(datetime.date(day, month, year)))

then I have to check if that date is a business end of month, if yes the code continues if not it exists. I tried this:

if date1 == BMonthEnd():
    pass
else:
    print("Ricontrolla: " + str(date1) + " non è un fine mese")
    exit(0)

here I get the error as if I input 29-05-2020 the code exit even if this date is a business end of month (its a friday). Thanks. Luigi

Luigi87
  • 265
  • 4
  • 13
  • related: https://stackoverflow.com/questions/37441323/finding-last-business-day-of-a-month-in-python – FObersteiner Jul 01 '20 at 15:16
  • yes that is a cleaner way I admit. Anyway that was already working, any idea on how to solve the problem?thanks – Luigi87 Jul 01 '20 at 15:17

1 Answers1

1

you could use the rollforwardmethod to check if you get the same date returned as date1:

import pandas as pd

date1 = pd.to_datetime('28-05-2020', dayfirst=True)

bm_end = pd.tseries.offsets.BMonthEnd()

if date1 == bm_end.rollforward(date1):
    pass
else:
    print("Ricontrolla: " + str(date1) + " non è un fine mese")
    
# prints
# Ricontrolla: 2020-05-28 00:00:00 non è un fine mese
FObersteiner
  • 22,500
  • 8
  • 42
  • 72
  • @Luigi87: then you might want to clarify the question. This answer shows how to implement a logic to check if a given input date is equal to a business month's end date. Slicing a DataFrame based on the result sounds like a pretty different subject to me. – FObersteiner Jul 01 '20 at 15:25