Categorical.from_codes should validate ordered argument · Issue #14058 · pandas-dev/pandas (original) (raw)
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