Pandas – Boolean value conditional statement not being picked up in function

New to python.

I have a dataset containing a date column formatted as yyyy-mm-dd (%Y%m%d) as datetime64 type. The dataset spans 2 years 2019-2020. I’m trying to write a function that will add the quarter based on the date. I can’t get the if statements to recognize the data so everything is coming back as ‘Q42020’ and I don’t understand why.

def applyquarter(x):
        if 'date' < '2019-04-01':
            return('Q12019')
        elif 'date' < '2019-07-01':
            return('Q22019')
        elif 'date' < '2019-10-01':
            return('Q32019')
        elif 'date' < '2020-01-01':
            return('Q42019')
        elif 'date' < '2020-04-01':
            return('Q12020')
        elif 'date' < '2020-07-01':
            return('Q22020')
        elif 'date' < '2020-10-01':
            return('Q32020')
        else: 
            return('Q42020')  

My understanding:

if/elif/else statements run until the criteria is met, then stop running

datetime64 can be used with boolean operators

the problem must exist in the boolean interaction with the datetime value

Would anyone please explain what I’m not understanding correctly?

Answer

    if 'date' < '2019-04-01':

This compares two character strings. Nothing in your posted code makes any reference to a data frame.

See here for converting a date to a quarter. The critical call is

pd.PeriodIndex(df.date, freq='Q')