def _set_grouper(self, obj, sort=False):
if self.key is not None and self.level is not None:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>")<EOL><DEDENT>if self._grouper is None:<EOL><INDENT>self._grouper = self.grouper<EOL><DEDENT>if self.key is not None:<EOL><INDENT>key = self.key<EOL>if (getattr(self.grouper, '<STR_LIT:name>', None) == key and<EOL>isinstance(obj, ABCSeries)):<EOL><INDENT>ax = self._grouper.take(obj.index)<EOL><DEDENT>else:<EOL><INDENT>if key not in obj._info_axis:<EOL><INDENT>raise KeyError(<EOL>"<STR_LIT>".format(key))<EOL><DEDENT>ax = Index(obj[key], name=key)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>ax = obj._get_axis(self.axis)<EOL>if self.level is not None:<EOL><INDENT>level = self.level<EOL>if isinstance(ax, MultiIndex):<EOL><INDENT>level = ax._get_level_number(level)<EOL>ax = Index(ax._get_level_values(level),<EOL>name=ax.names[level])<EOL><DEDENT>else:<EOL><INDENT>if level not in (<NUM_LIT:0>, ax.name):<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>".format(level))<EOL><DEDENT><DEDENT><DEDENT><DEDENT>if (self.sort or sort) and not ax.is_monotonic:<EOL><INDENT>indexer = self.indexer = ax.argsort(kind='<STR_LIT>')<EOL>ax = ax.take(indexer)<EOL>obj = obj._take(indexer, axis=self.axis, is_copy=False)<EOL><DEDENT>self.obj = obj<EOL>self.grouper = ax<EOL>return self.grouper<EOL>
given an object and the specifications, setup the internal grouper
for this particular specification

Parameters
----------
obj : the subject object
sort : bool, default False
    whether the resulting grouper should be sorted