Trunks
Trunks

Reputation: 151

TypeError: unsupported operand type(s) for &: 'DatetimeArray' and 'DatetimeArray' - Pandas

I want to filter the DF by checking the PCS Column if == 0 and 'ITEM DESC' =='ITEM XL'. If the criteria is met, I will multiply it by the BOX value and multiply it by 48.

DF

     ITEM DESC            PCS                   BOX
     ITEM S               12                     -   
     ITEM M               12                     -   
     ITEM L               12                     -   
     ITEM XS              12                     -   
     ITEM XL              0                      1 
     

CODES

    if DF[DF['PCS']==0] & DF[DF['ITEM DESC']=='ITEM XL'] :  
       DF['PCS'] = DF['PCS'] * DF['BOX'] * 48   
    else:
       DF['PCS'] 

ERROR

      TypeError: unsupported operand type(s) for &: 'DatetimeArray' and 'DatetimeArray'

Upvotes: 1

Views: 638

Answers (2)

U13-Forward
U13-Forward

Reputation: 71570

Either you meant all:

if DF[(DF['PCS']==0) & (DF['ITEM DESC']=='ITEM XL')].all():  
   DF['PCS'] = DF['PCS'] * DF['BOX'] * 48   

Or any:

if DF[(DF['PCS']==0) & (DF['ITEM DESC']=='ITEM XL')].any():  
   DF['PCS'] = DF['PCS'] * DF['BOX'] * 48     

Or more probable that you want to do it to specific rows:

 DF.loc[(DF['PCS']==0) & (DF['ITEM DESC']=='ITEM XL'), 'PCS'] = DF['PCS'] * DF['BOX'] * 48

Upvotes: 1

The Singularity
The Singularity

Reputation: 2698

Try using .apply

DF['PCS'] = DF.apply(lambda x: x.DF['PCS'] * x.DF['BOX'] * 48 if x.DF['PCS']==0 and x.DF['ITEM DESC']=='ITEM XL' else x.DF['PCS']==0, axis=1)

Upvotes: 0

Related Questions