Home > Software design >  Why am I getting: TypeError: unsupported operand type(s) for -: ‘str’ and ‘str’ even though dtypes i
Why am I getting: TypeError: unsupported operand type(s) for -: ‘str’ and ‘str’ even though dtypes i

Time:12-10

Excuse the messy code but I am working with this function:

def zip_zap(diction):
    zipzap = {}
    #key = nwe     pair = list of dfs
    for key, pair in diction.items():
        elist= []
        aftere = []
        z3 = []
        team = zipper[key]
        #for game in list of team games
        for i in pair[0:12]:
             elist.append(i)
             for e in elist:
  
                if e.columns.nlevels > 1:
                    e.columns = e.columns.droplevel()

             e.apply(pd.to_numeric, errors = 'ignore')
  
             aftere.append(e.iloc[:,1:])
             for i in aftere:
                 print(i.dtypes)

When I run this I get this output for every dataframe. Indicating that all the columns have type float64:

Total float64 Tot float64 Pass float64 Rush float64 TOvr float64 Tot float64 Pass float64 Rush float64 TOvr float64 Tot float64 KO float64 KR float64 P float64 PR float64 FG/XP float64 dtype: object

But when I try to find the difference between rows I get an error saying that I cant subtract a string from a string

def zip_zap(diction):
zipzap = {}
#key = nwe     pair = list of dfs
for key, pair in diction.items():
    elist= []
    aftere = []
    z3 = []
    team = zipper[key]
    #for game in list of team games
    for i in pair[0:12]:
         elist.append(i)
         for e in elist:

            if e.columns.nlevels > 1:
                e.columns = e.columns.droplevel()

         e.apply(pd.to_numeric, errors = 'ignore')

         aftere.append(e.iloc[:,1:])
         for i in aftere:
             differ = i.diff()
             z3.append(differ)

This is my error output:

TypeError                                 Traceback (most recent call last)
<ipython-input-140-a97f786b4bbb> in <module>
----> 1 zip_zap(zipper)

<ipython-input-139-bcb723143a3f> in zip_zap(diction)
     25                 for i in aftere:
     26 
---> 27                     differ = i.diff()
     28                     z3.append(differ)
     29     #             #Create Tm column and insert at first position so that we can concat the dfs

~\anaconda3\lib\site-packages\pandas\core\frame.py in diff(self, periods, axis)
   7254             return self.T.diff(periods, axis=0).T
   7255 
-> 7256         new_data = self._mgr.diff(n=periods, axis=bm_axis)
   7257         return self._constructor(new_data)
   7258 

~\anaconda3\lib\site-packages\pandas\core\internals\managers.py in diff(self, n, axis)
    556 
    557     def diff(self, n: int, axis: int) -> "BlockManager":
--> 558         return self.apply("diff", n=n, axis=axis)
    559 
    560     def interpolate(self, **kwargs) -> "BlockManager":

~\anaconda3\lib\site-packages\pandas\core\internals\managers.py in apply(self, f, align_keys, **kwargs)
    404                 applied = b.apply(f, **kwargs)
    405             else:
--> 406                 applied = getattr(b, f)(**kwargs)
    407             result_blocks = _extend_blocks(applied, result_blocks)
    408 

~\anaconda3\lib\site-packages\pandas\core\internals\blocks.py in diff(self, n, axis)
   1270     def diff(self, n: int, axis: int = 1) -> List["Block"]:
   1271         """ return block for the diff of the values """
-> 1272         new_values = algos.diff(self.values, n, axis=axis, stacklevel=7)
   1273         return [self.make_block(values=new_values)]
   1274 

~\anaconda3\lib\site-packages\pandas\core\algorithms.py in diff(arr, n, axis, stacklevel)
   1975             out_arr[res_indexer] = arr[res_indexer] ^ arr[lag_indexer]
   1976         else:
-> 1977             out_arr[res_indexer] = arr[res_indexer] - arr[lag_indexer]
   1978 
   1979     if is_timedelta:

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

CodePudding user response:

Change this line:

e.apply(pd.to_numeric, errors = 'ignore')

to this:

e = e.apply(pd.to_numeric, errors = 'ignore')

Because apply() returns a new series/dataframe with the changes; it doesn't modify the existing one.

  • Related