Home > Net >  Why do I get "TypeError: unsupported operand type(s) for -: 'str' and 'str'
Why do I get "TypeError: unsupported operand type(s) for -: 'str' and 'str'

Time:11-14

This is my DataFrame:

           Date        Open        High  ...       Close   Adj Close     Volume
0    11/12/2020  119.620003  120.529999  ...  119.209999  118.479599  103162300
1    11/13/2020  119.440002  119.669998  ...  119.260002  118.529289   81581900
2    11/16/2020  118.919998  120.989998  ...  120.300003  119.562920   91183000

..          ...         ...         ...  ...         ...         ...        ...

252  11/12/2021  148.429993  150.395004  ...  149.990005  149.990005   52814971

And I am trying to run this simple code on it:

for i in range(1, dataset.shape[0]):
  dataset_new.loc[i-1 , :] =  dataset.loc[i , :] - dataset.loc[i-1 , :]

But I get this error message:

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

I don't know why this happens and how can I solve it?

CodePudding user response:

Your date column is of string type, most probably. You need to make sure that all the columns are subtractable..dataset_new.dtypes will print out the column types for you.

  • Related