variable transformation using conditions not working properly, how to debug?

‘m working with frames and I’m trying to transform a variable where months and dates are strings into a new variable season. However my if conditional statements below will only make a new var season where everything is spring (probably because it’s the first if statement), everything else is ignored.

Anyone got any clue as to why?

for i in labels['str_date'].fillna('-1'):
    month = i.split()[0]
    #most dates = september,sept or august,aug, nans as -1 
    if month in ('september','sept'):
        labels['season'] = 'spring'
    if month in ('august','aug'):
        labels['season'] = 'winter'
        labels['season'] = 'no date'

enter image description here


You’re assigning each time to the entire column.

The way to fix would be using nested np.wheres:

x = labels['str_date'].fillna('-1')
labels['season'] = np.where(x.str.split().str[0].isin(('september','sept')), 'spring', np.where(x.str.split().str[0].isin(('august','aug')), 'winter', 'no date'))

Or even better with

x = labels['str_date'].fillna('-1')
labels['season'] =[x.str.split().str[0].isin(('september','sept')), x.str.split().str[0].isin(('august','aug'))], ['spring', 'winter'], default='no date')