Not able to execute the following code in python version '3.5.1'

hackathon
python

#1

Hi Guys,
we are new to python and this is our first project
we have a problem with respect to the following code
"outlet_size_mode = data.pivot_table(values=’Outlet_Size’, columns=’Outlet_Type’,aggfunc=(lambda x:mode(x).mode[0]) )”

we are getting the following error

RuntimeWarning: The input array could not be properly checked for nan values. nan values will be ignored.
“values. nan values will be ignored.”, RuntimeWarning)

ValueError Traceback (most recent call last)
C:\Users\nagarjun\Anaconda3\lib\site-packages\pandas\core\groupby.py in aggregate(self, arg, *args, **kwargs)
2983 assert not args and not kwargs
-> 2984 result = self._aggregate_multiple_funcs([arg])
2985 result.columns = Index(result.columns.levels[0],

C:\Users\nagarjun\Anaconda3\lib\site-packages\pandas\core\groupby.py in _aggregate_multiple_funcs(self, arg)
3015 raise
-> 3016 result = concat(results, keys=keys, axis=1)
3017

C:\Users\nagarjun\Anaconda3\lib\site-packages\pandas\tools\merge.py in concat(objs, axis, join, join_axes, ignore_index, keys, levels, names, verify_integrity, copy)
811 verify_integrity=verify_integrity,
–> 812 copy=copy)
813 return op.get_result()

C:\Users\nagarjun\Anaconda3\lib\site-packages\pandas\tools\merge.py in init(self, objs, axis, join, join_axes, keys, levels, names, ignore_index, verify_integrity, copy)
844 if len(objs) == 0:
–> 845 raise ValueError(‘No objects to concatenate’)
846

ValueError: No objects to concatenate

During handling of the above exception, another exception occurred:

TypeError Traceback (most recent call last)
C:\Users\nagarjun\Anaconda3\lib\site-packages\pandas\core\groupby.py in _aggregate_generic(self, func, *args, **kwargs)
3030 for name, data in self:
-> 3031 result[name] = self._try_cast(func(data, *args, **kwargs),
3032 data)

in (x)
----> 1 outlet_size_mode = trainandtest.pivot_table(values=‘Outlet_Size’, columns=‘Outlet_Type’,aggfunc=(lambda x:mode(x).mode[0]) )

C:\Users\nagarjun\Anaconda3\lib\site-packages\scipy\stats\stats.py in mode(a, axis, nan_policy)
643
–> 644 scores = np.unique(np.ravel(a)) # get ALL unique values
645 testshape = list(a.shape)

C:\Users\nagarjun\Anaconda3\lib\site-packages\numpy\lib\arraysetops.py in unique(ar, return_index, return_inverse, return_counts)
197 else:
–> 198 ar.sort()
199 aux = ar

TypeError: unorderable types: str() > float()

During handling of the above exception, another exception occurred:

TypeError Traceback (most recent call last)
in ()
----> 1 outlet_size_mode = trainandtest.pivot_table(values=‘Outlet_Size’, columns=‘Outlet_Type’,aggfunc=(lambda x:mode(x).mode[0]) )


C:\Users\nagarjun\Anaconda3\lib\site-packages\pandas\tools\pivot.py in pivot_table(data, values, index, columns, aggfunc, fill_value, margins, dropna, margins_name)
112
113 grouped = data.groupby(keys)
–> 114 agged = grouped.agg(aggfunc)
115
116 table = agged

C:\Users\nagarjun\Anaconda3\lib\site-packages\pandas\core\groupby.py in agg(self, func, *args, **kwargs)
727 @Appender(_agg_doc)
728 def agg(self, func, *args, **kwargs):
–> 729 return self.aggregate(func, *args, **kwargs)
730
731 def _iterate_slices(self):

C:\Users\nagarjun\Anaconda3\lib\site-packages\pandas\core\groupby.py in aggregate(self, arg, *args, **kwargs)
2986 name=self._selected_obj.columns.name)
2987 except:
-> 2988 result = self._aggregate_generic(arg, *args, **kwargs)
2989
2990 if not self.as_index:

C:\Users\nagarjun\Anaconda3\lib\site-packages\pandas\core\groupby.py in _aggregate_generic(self, func, *args, **kwargs)
3032 data)
3033 except Exception:
-> 3034 return self._aggregate_item_by_item(func, *args, **kwargs)
3035 else:
3036 for name in self.indices:

C:\Users\nagarjun\Anaconda3\lib\site-packages\pandas\core\groupby.py in _aggregate_item_by_item(self, func, *args, **kwargs)
3076 # GH6337
3077 if not len(result_columns) and errors is not None:
-> 3078 raise errors
3079
3080 return DataFrame(result, columns=result_columns)

C:\Users\nagarjun\Anaconda3\lib\site-packages\pandas\core\groupby.py in _aggregate_item_by_item(self, func, *args, **kwargs)
3061 grouper=self.grouper)
3062 result[item] = self._try_cast(
-> 3063 colg.aggregate(func, *args, **kwargs), data)
3064 except ValueError:
3065 cannot_agg.append(item)

C:\Users\nagarjun\Anaconda3\lib\site-packages\pandas\core\groupby.py in aggregate(self, func_or_funcs, *args, **kwargs)
2438 return self._python_agg_general(func_or_funcs, *args, **kwargs)
2439 except Exception:
-> 2440 result = self._aggregate_named(func_or_funcs, *args, **kwargs)
2441
2442 index = Index(sorted(result), name=self.grouper.names[0])

C:\Users\nagarjun\Anaconda3\lib\site-packages\pandas\core\groupby.py in _aggregate_named(self, func, *args, **kwargs)
2534 for name, group in self:
2535 group.name = name
-> 2536 output = func(group, *args, **kwargs)
2537 if isinstance(output, (Series, Index, np.ndarray)):
2538 raise Exception(‘Must produce aggregated value’)

in (x)
----> 1 outlet_size_mode = trainandtest.pivot_table(values=‘Outlet_Size’, columns=‘Outlet_Type’,aggfunc=(lambda x:mode(x).mode[0]) )

C:\Users\nagarjun\Anaconda3\lib\site-packages\scipy\stats\stats.py in mode(a, axis, nan_policy)
642 return mstats_basic.mode(a, axis)
643
–> 644 scores = np.unique(np.ravel(a)) # get ALL unique values
645 testshape = list(a.shape)
646 testshape[axis] = 1

C:\Users\nagarjun\Anaconda3\lib\site-packages\numpy\lib\arraysetops.py in unique(ar, return_index, return_inverse, return_counts)
196 aux = ar[perm]
197 else:
–> 198 ar.sort()
199 aux = ar
200 flag = np.concatenate(([True], aux[1:] != aux[:-1]))

TypeError: unorderable types: float() > str()


as per Aarshay Jain response we executed all the code and got the same error. we did some research and got some hints towards the problem is with version

Can anyone plz help

Thanks
Nagarjun Srinivas


#2

Try posting this error in the mailing list of pandas. Maybe they could help.


#3

I am getting the same error… is that problem solve…please help me ;how to remove the error


#4

data.dropna(subset=[‘Outlet_Size’]).pivot_table(values=‘Outlet_Size’, columns=‘Outlet_Type’,aggfunc=(lambda x:mode(x).mode[0]), dropna=True)

it works for me


#5

The Null values are being represented by numpy.nan. The numpy.nan is of float type.
While performing sorting (inside stats.mode() call), the comparator is unable to compare ‘float’ with ‘str’.

Try this:
mode(x.astype('str')).mode[0]

It converts the data type of values (in x) from object to string before calculating the mode.


#6

It is not the correct way. because in this case it will consider nan as a mode value for Grocery store.