Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str'
#1
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!!
buran wrote Sep-18-2019, 07:44 AM:
Please, always post full traceback you get, in error tags

Also all relevant code - in this case we can only guess that PRESENT is of type str
Quote
#2
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'
Quote
#3
check your data - you have str values where you expect to have numbers
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  TypeError: size; expecting a recognized type filling string dict a11_m11 0 176 Feb-10-2020, 08:26 AM
Last Post: a11_m11
  TypeError: type str doesn't define __round__ method emmapaw24 7 481 Feb-03-2020, 08:38 PM
Last Post: snippsat
  TypeError: can't multiply sequence by non-int of type 'str' emmapaw24 2 147 Feb-03-2020, 05:50 PM
Last Post: michael1789
  Type hinting - return type based on parameter micseydel 2 149 Jan-14-2020, 01:20 AM
Last Post: micseydel
  Python DateTime is broken 10OctNotOct10a1 8 450 Jan-03-2020, 07:54 AM
Last Post: snippsat
  Confusion with datetime 'dst' function result jsmith1703 4 315 Nov-16-2019, 02:55 AM
Last Post: DeaD_EyE
  convert strings of date to datetime exported from CSV GiorgosPap31 1 234 Oct-31-2019, 02:37 PM
Last Post: buran
  Need help with "unsupported operand type(s)" _dave 6 841 Oct-14-2019, 06:43 AM
Last Post: buran
  TypeError: unsupported operand type(s) for -: 'str' and 'str' Balaji 4 1,514 Oct-11-2019, 10:29 AM
Last Post: buran
  importing datetime trouble Dixon 4 1,419 Aug-24-2019, 11:47 AM
Last Post: satishm

Forum Jump:


Users browsing this thread: 1 Guest(s)