numpy.str_
s as catgories · Issue #31499 · pandas-dev/pandas (original) (raw)
Pandas 1.0 no longer handles Code Sample
import pandas as pd pd.Categorical(['1', '0', '1'], [np.str_('0'), np.str_('1')])
Traceback (most recent call last): File "", line 1, in File "/home/angerer/Dev/Python/venvs/env-pandas-1/lib/python3.8/site-packages/pandas/core/arrays/categorical.py", line 385, in init codes = _get_codes_for_values(values, dtype.categories) File "/home/angerer/Dev/Python/venvs/env-pandas-1/lib/python3.8/site-packages/pandas/core/arrays/categorical.py", line 2576, in _get_codes_for_values t.map_locations(cats) File "pandas/_libs/hashtable_class_helper.pxi", line 1403, in pandas.libs.hashtable.StringHashTable.map_locations TypeError: Expected unicode, got numpy.str
Problem description
I know that having a list of numpy.str_
s seems weird, but it easily happens when you use non-numpy algorithms on numpy arrays (e.g. natsort.natsorted
in our case), or via comprehensions or so:
np.array(['1', '0'])[0].class <class 'numpy.str_'> [type(s) for s in np.array(['1', '0'])] [<class 'numpy.str_'>, <class 'numpy.str_'>]
Expected Output
A normal pd.Categorical
Pandas version
pandas 1.0