BUG: astype('category', categories=...) failes on a series of categorical type · Issue #10696 · pandas-dev/pandas (original) (raw)
s.astype('category', categories=['a', 'b', 'c'])
fails when the series is already of Categorical dtype:
I am not sure if this should work (it would then be equivalent to set_categories
?), but in any case the current error message is not informative:
In [49]: s = pd.Series(['a', 'b', 'a'])
In [50]: s
Out[50]:
0 a
1 b
2 a
dtype: object
In [51]: s.astype('category')
Out[51]:
0 a
1 b
2 a
dtype: category
Categories (2, object): [a, b]
In [52]: s.astype('category', categories=['a', 'b', 'c'])
Out[52]:
0 a
1 b
2 a
dtype: category
Categories (3, object): [a, b, c]
In [53]: scat = s.astype('category')
In [54]: scat.astype('category', categories=['a', 'b', 'c'])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-54-f955e6286a85> in <module>()
----> 1 scat.astype('category', categories=['a', 'b', 'c'])
c:\users\vdbosscj\scipy\pandas-joris\pandas\core\generic.pyc in astype(self, dty
pe, copy, raise_on_error, **kwargs)
2415
2416 mgr = self._data.astype(
-> 2417 dtype=dtype, copy=copy, raise_on_error=raise_on_error, **kwa
rgs)
2418 return self._constructor(mgr).__finalize__(self)
2419
c:\users\vdbosscj\scipy\pandas-joris\pandas\core\internals.pyc in astype(self, d
type, **kwargs)
2516
2517 def astype(self, dtype, **kwargs):
-> 2518 return self.apply('astype', dtype=dtype, **kwargs)
2519
2520 def convert(self, **kwargs):
c:\users\vdbosscj\scipy\pandas-joris\pandas\core\internals.pyc in apply(self, f,
axes, filter, do_integrity_check, **kwargs)
2471 copy=align_copy)
2472
-> 2473 applied = getattr(b, f)(**kwargs)
2474
2475 if isinstance(applied, list):
c:\users\vdbosscj\scipy\pandas-joris\pandas\core\internals.pyc in astype(self, d
type, copy, raise_on_error, values, **kwargs)
371 def astype(self, dtype, copy=False, raise_on_error=True, values=None
, **kwargs):
372 return self._astype(dtype, copy=copy, raise_on_error=raise_on_er
ror,
--> 373 values=values, **kwargs)
374
375 def _astype(self, dtype, copy=False, raise_on_error=True, values=Non
e,
TypeError: _astype() got an unexpected keyword argument 'categories'