Skip to content

Categorical.from_codes should validate ordered argument #14058

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
TomAugspurger opened this issue Aug 21, 2016 · 2 comments
Closed

Categorical.from_codes should validate ordered argument #14058

TomAugspurger opened this issue Aug 21, 2016 · 2 comments
Labels
Categorical Categorical Data Type Error Reporting Incorrect or improved errors from pandas
Milestone

Comments

@TomAugspurger
Copy link
Contributor

I ran into this when I accidentally passed my codes to the ordered argument

Code Sample, a copy-pastable example if possible

In [2]: pd.Categorical.from_codes([0, 0, 1], categories=['a', 'b', 'c'], ordered=np.array([0, 1, 2]))
Out[2]: ---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/Users/tom.augspurger/Envs/py3/lib/python3.5/site-packages/IPython/core/formatters.py in __call__(self, obj)
    668                 type_pprinters=self.type_printers,
    669                 deferred_pprinters=self.deferred_printers)
--> 670             printer.pretty(obj)
    671             printer.flush()
    672             return stream.getvalue()

/Users/tom.augspurger/Envs/py3/lib/python3.5/site-packages/IPython/lib/pretty.py in pretty(self, obj)
    381                             if callable(meth):
    382                                 return meth(obj, self, cycle)
--> 383             return _default_pprint(obj, self, cycle)
    384         finally:
    385             self.end_group()

/Users/tom.augspurger/Envs/py3/lib/python3.5/site-packages/IPython/lib/pretty.py in _default_pprint(obj, p, cycle)
    501     if _safe_getattr(klass, '__repr__', None) not in _baseclass_reprs:
    502         # A user-provided repr. Find newlines and replace them with p.break_()
--> 503         _repr_pprint(obj, p, cycle)
    504         return
    505     p.begin_group(1, '<')

/Users/tom.augspurger/Envs/py3/lib/python3.5/site-packages/IPython/lib/pretty.py in _repr_pprint(obj, p, cycle)
    692     """A pprint that just redirects to the normal repr function."""
    693     # Find newlines and replace them with p.break_()
--> 694     output = repr(obj)
    695     for idx,output_line in enumerate(output.splitlines()):
    696         if idx:

/Users/tom.augspurger/Envs/py3/lib/python3.5/site-packages/pandas/pandas/core/base.py in __repr__(self)
     70         Yields Bytestring in Py2, Unicode String in py3.
     71         """
---> 72         return str(self)
     73
     74

/Users/tom.augspurger/Envs/py3/lib/python3.5/site-packages/pandas/pandas/core/base.py in __str__(self)
     49
     50         if compat.PY3:
---> 51             return self.__unicode__()
     52         return self.__bytes__()
     53

/Users/tom.augspurger/Envs/py3/lib/python3.5/site-packages/pandas/pandas/core/categorical.py in __unicode__(self)
   1571             result = self._tidy_repr(_maxlen)
   1572         elif len(self._codes) > 0:
-> 1573             result = self._get_repr(length=len(self) > _maxlen)
   1574         else:
   1575             result = ('[], %s' %

/Users/tom.augspurger/Envs/py3/lib/python3.5/site-packages/pandas/pandas/core/categorical.py in _get_repr(self, length, na_rep, footer)
   1562         formatter = fmt.CategoricalFormatter(self, length=length,
   1563                                              na_rep=na_rep, footer=footer)
-> 1564         result = formatter.to_string()
   1565         return compat.text_type(result)
   1566

/Users/tom.augspurger/Envs/py3/lib/python3.5/site-packages/pandas/pandas/formats/format.py in to_string(self)
    134         result = [u('[') + result + u(']')]
    135         if self.footer:
--> 136             footer = self._get_footer()
    137             if footer:
    138                 result.append(footer)

/Users/tom.augspurger/Envs/py3/lib/python3.5/site-packages/pandas/pandas/formats/format.py in _get_footer(self)
    105             footer += "Length: %d" % len(self.categorical)
    106
--> 107         level_info = self.categorical._repr_categories_info()
    108
    109         # Levels are added in a newline

/Users/tom.augspurger/Envs/py3/lib/python3.5/site-packages/pandas/pandas/core/categorical.py in _repr_categories_info(self)
   1540         start = True
   1541         cur_col_len = len(levheader)  # header
-> 1542         sep_len, sep = (3, " < ") if self.ordered else (2, ", ")
   1543         linesep = sep.rstrip() + "\n"  # remove whitespace
   1544         for val in category_strs:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

If you look through the traceback, the error happens in the repr, not at construction time.
The regular Categorical constructor does catch this (error message could be improved though).

Expected Output

TypeError("orderedmust be a boolean, got {!r} instead")

output of pd.show_versions()

master

@TomAugspurger TomAugspurger added Error Reporting Incorrect or improved errors from pandas Categorical Categorical Data Type labels Aug 21, 2016
@TomAugspurger TomAugspurger added this to the 0.20.0 milestone Aug 21, 2016
gfyoung added a commit to forking-repos/pandas that referenced this issue Aug 21, 2016
@gfyoung
Copy link
Member

gfyoung commented Aug 21, 2016

@TomAugspurger : IMO this could go in for 0.19.0.

@TomAugspurger
Copy link
Contributor Author

Agreed it should be a very minor fix. If someone has a chance and submits a PR then we can move it forward.

On Aug 21, 2016, at 12:38, gfyoung [email protected] wrote:

@TomAugspurger : IMO this could go in for 0.19.0.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Categorical Categorical Data Type Error Reporting Incorrect or improved errors from pandas
Projects
None yet
Development

No branches or pull requests

3 participants