Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: "unsupported operand type(s) for -: 'Timestamp' and 'str'" pandas

I am working on a project and so far my code looks like this:

def vacation_date(row):
if pd.isnull(row['vacation_date']) == False :
    return float((row['vacation_date'] - row['purchased_trip_date']).days)
else:
    pass 
preprocessed_data['vacation_date'] = data.apply(lambda row:
vacation_date(row), axis=1)

it is returning: TypeError: ("unsupported operand type(s) for -: 'Timestamp' and 'str'", 'occurred at index 3')

I am not sure what I need to do to fix this, any suggestions are appreciated, thanks!

like image 542
kwashington122 Avatar asked Mar 30 '17 16:03

kwashington122


1 Answers

The error message is one of the clearest I've ever seen in computing - it's telling you that you're trying to subtract a string from a Timestamp. Since what you're subtracting is row['purchased_trip_date'], it means this is a string. Convert it to another Timestamp first.

like image 123
Mark Ransom Avatar answered Nov 16 '22 23:11

Mark Ransom