Skip to content

Regression: "invalid type <class 'pandas.core.dtypes.dtypes.CategoricalDtype'> for astype" #17780

Closed
@toobaz

Description

@toobaz

Sorry in advance if this is actually desired behaviour. The whatsnew/v0.21.0.txt only states

"passing categories or ordered kwargs to :func:Series.astype is deprecated, in favor of passing a :ref:CategoricalDtype <whatsnew_0210.enhancements.categorical_dtype> (:issue:#17636)"

... so I assume it is not.

Code Sample, a copy-pastable example if possible

Until recently (way more recently than this particular git commit):

In [2]: pd.__version__
Out[2]: '0.19.0+git14-ga40e185'

In [3]: pd.Series(['a', 'b', 'c']).astype(pd.types.dtypes.CategoricalDtype)
Out[3]: 
0    a
1    b
2    c
dtype: object

Now:

In [2]: pd.Series(['a', 'b', 'c']).astype(pd.core.dtypes.dtypes.CategoricalDtype)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-512aeb10c4b0> in <module>()
----> 1 pd.Series(['a', 'b', 'c']).astype(pd.core.dtypes.dtypes.CategoricalDtype)

/home/pietro/nobackup/repo/pandas/pandas/util/_decorators.py in wrapper(*args, **kwargs)
    115                 else:
    116                     kwargs[new_arg_name] = new_arg_value
--> 117             return func(*args, **kwargs)
    118         return wrapper
    119     return _deprecate_kwarg

/home/pietro/nobackup/repo/pandas/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
   3898         # else, only a single dtype is given
   3899         new_data = self._data.astype(dtype=dtype, copy=copy, errors=errors,
-> 3900                                      **kwargs)
   3901         return self._constructor(new_data).__finalize__(self)
   3902 

/home/pietro/nobackup/repo/pandas/pandas/core/internals.py in astype(self, dtype, **kwargs)
   3404 
   3405     def astype(self, dtype, **kwargs):
-> 3406         return self.apply('astype', dtype=dtype, **kwargs)
   3407 
   3408     def convert(self, **kwargs):

/home/pietro/nobackup/repo/pandas/pandas/core/internals.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
   3271 
   3272             kwargs['mgr'] = self
-> 3273             applied = getattr(b, f)(**kwargs)
   3274             result_blocks = _extend_blocks(applied, result_blocks)
   3275 

/home/pietro/nobackup/repo/pandas/pandas/core/internals.py in astype(self, dtype, copy, errors, values, **kwargs)
    530     def astype(self, dtype, copy=False, errors='raise', values=None, **kwargs):
    531         return self._astype(dtype, copy=copy, errors=errors, values=values,
--> 532                             **kwargs)
    533 
    534     def _astype(self, dtype, copy=False, errors='raise', values=None,

/home/pietro/nobackup/repo/pandas/pandas/core/internals.py in _astype(self, dtype, copy, errors, values, klass, mgr, raise_on_error, **kwargs)
    548         # may need to convert to categorical
    549         # this is only called for non-categoricals
--> 550         if self.is_categorical_astype(dtype):
    551 
    552             # deprecated 17636

/home/pietro/nobackup/repo/pandas/pandas/core/internals.py in is_categorical_astype(self, dtype)
    143             # this is a pd.Categorical, but is not
    144             # a valid type for astypeing
--> 145             raise TypeError("invalid type {0} for astype".format(dtype))
    146 
    147         elif is_categorical_dtype(dtype):

TypeError: invalid type <class 'pandas.core.dtypes.dtypes.CategoricalDtype'> for astype

Problem description

Again, not really sure this is undesired... maybe it would be enough to clarify a bit the whatsnew note.

Expected Output

Previous behaviour.

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.5.3.final.0
python-bits: 64
OS: Linux
OS-release: 4.9.0-3-amd64
machine: x86_64
processor:
byteorder: little
LC_ALL: None
LANG: en_GB.UTF-8
LOCALE: en_GB.UTF-8

pandas: 0.21.0.dev+572.g8e89cb3e1
pytest: 3.0.6
pip: 9.0.1
setuptools: 33.1.1
Cython: 0.25.2
numpy: 1.12.1
scipy: 0.18.1
pyarrow: None
xarray: None
IPython: 5.2.2
sphinx: None
patsy: 0.4.1+dev
dateutil: 2.6.0
pytz: 2016.10
blosc: None
bottleneck: None
tables: 3.3.0
numexpr: 2.6.1
feather: 0.3.1
matplotlib: 2.0.0
openpyxl: 2.3.0
xlrd: 1.0.0
xlwt: 1.2.0
xlsxwriter: None
lxml: 3.7.1
bs4: 4.5.3
html5lib: 0.999999999
sqlalchemy: 1.0.15
pymysql: None
psycopg2: None
jinja2: 2.8
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    CategoricalCategorical Data TypeError ReportingIncorrect or improved errors from pandas

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions