Python Forum

Full Version: TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Can any one help me on this , unable to determince the reason behind the error while performing RFM analysis.

rfm= data_filtered.groupby('ProductID').agg({'Transaction Time': lambda date: (PRESENT - date.max()).days,
                                        'RetailStore': lambda num: len(num),
                                        'Cost': lambda price: price.sum()})
HELP NEEDED!!
Error:
TypeError Traceback (most recent call last) ~\Anaconda3\lib\site-packages\pandas\core\groupby\groupby.py in aggregate(self, func_or_funcs, *args, **kwargs) 3494 try: -> 3495 return self._python_agg_general(func_or_funcs, *args, **kwargs) 3496 except Exception: ~\Anaconda3\lib\site-packages\pandas\core\groupby\groupby.py in _python_agg_general(self, func, *args, **kwargs) 1073 if len(output) == 0: -> 1074 return self._python_apply_general(f) 1075 ~\Anaconda3\lib\site-packages\pandas\core\groupby\groupby.py in _python_apply_general(self, f) 935 keys, values, mutated = self.grouper.apply(f, self._selected_obj, --> 936 self.axis) 937 ~\Anaconda3\lib\site-packages\pandas\core\groupby\groupby.py in apply(self, f, data, axis) 2272 group_axes = _get_axes(group) -> 2273 res = f(group) 2274 if not _is_indexed_like(res, group_axes): ~\Anaconda3\lib\site-packages\pandas\core\groupby\groupby.py in <lambda>(x) 1061 func = self._is_builtin_func(func) -> 1062 f = lambda x: func(x, *args, **kwargs) 1063 <ipython-input-30-c2dd1078948c> in <lambda>(date) ----> 1 rfm= data_filtered.groupby('ProductID').agg({'Transaction Time': lambda date: (PRESENT - date.max()).days, 2 'RetailStore': lambda num: len(num), 3 'Cost': lambda price: price.sum()}) TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str' During handling of the above exception, another exception occurred: TypeError Traceback (most recent call last) <ipython-input-30-c2dd1078948c> in <module> 1 rfm= data_filtered.groupby('ProductID').agg({'Transaction Time': lambda date: (PRESENT - date.max()).days, 2 'RetailStore': lambda num: len(num), ----> 3 'Cost': lambda price: price.sum()}) ~\Anaconda3\lib\site-packages\pandas\core\groupby\groupby.py in aggregate(self, arg, *args, **kwargs) 4654 axis='')) 4655 def aggregate(self, arg, *args, **kwargs): -> 4656 return super(DataFrameGroupBy, self).aggregate(arg, *args, **kwargs) 4657 4658 agg = aggregate ~\Anaconda3\lib\site-packages\pandas\core\groupby\groupby.py in aggregate(self, arg, *args, **kwargs) 4085 4086 _level = kwargs.pop('_level', None) -> 4087 result, how = self._aggregate(arg, _level=_level, *args, **kwargs) 4088 if how is None: 4089 return result ~\Anaconda3\lib\site-packages\pandas\core\base.py in _aggregate(self, arg, *args, **kwargs) 488 489 try: --> 490 result = _agg(arg, _agg_1dim) 491 except SpecificationError: 492 ~\Anaconda3\lib\site-packages\pandas\core\base.py in _agg(arg, func) 439 result = compat.OrderedDict() 440 for fname, agg_how in compat.iteritems(arg): --> 441 result[fname] = func(fname, agg_how) 442 return result 443 ~\Anaconda3\lib\site-packages\pandas\core\base.py in _agg_1dim(name, how, subset) 422 raise SpecificationError("nested dictionary is ambiguous " 423 "in aggregation") --> 424 return colg.aggregate(how, _level=(_level or 0) + 1) 425 426 def _agg_2dim(name, how): ~\Anaconda3\lib\site-packages\pandas\core\groupby\groupby.py in aggregate(self, func_or_funcs, *args, **kwargs) 3495 return self._python_agg_general(func_or_funcs, *args, **kwargs) 3496 except Exception: -> 3497 result = self._aggregate_named(func_or_funcs, *args, **kwargs) 3498 3499 index = Index(sorted(result), name=self.grouper.names[0]) ~\Anaconda3\lib\site-packages\pandas\core\groupby\groupby.py in _aggregate_named(self, func, *args, **kwargs) 3623 for name, group in self: 3624 group.name = name -> 3625 output = func(group, *args, **kwargs) 3626 if isinstance(output, (Series, Index, np.ndarray)): 3627 raise Exception('Must produce aggregated value') <ipython-input-30-c2dd1078948c> in <lambda>(date) ----> 1 rfm= data_filtered.groupby('ProductID').agg({'Transaction Time': lambda date: (PRESENT - date.max()).days, 2 'RetailStore': lambda num: len(num), 3 'Cost': lambda price: price.sum()}) TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str'
check your data - you have str values where you expect to have numbers