-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Ensure Index.astype('category') returns a CategoricalIndex #18677
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
Changes from all commits
3c37bb7
6d953e4
20b5504
a90acec
afcc50a
6042131
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1053,6 +1053,10 @@ def _to_embed(self, keep_tz=False, dtype=None): | |
|
||
@Appender(_index_shared_docs['astype']) | ||
def astype(self, dtype, copy=True): | ||
if is_categorical_dtype(dtype): | ||
from .category import CategoricalIndex | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess we have an import issue if we import this at the top (with the fully qualified path)? |
||
return CategoricalIndex(self.values, name=self.name, dtype=dtype, | ||
copy=copy) | ||
return Index(self.values.astype(dtype, copy=copy), name=self.name, | ||
dtype=dtype) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,9 +14,11 @@ | |
from pandas.core.dtypes.common import ( | ||
_ensure_int64, | ||
_ensure_platform_int, | ||
is_categorical_dtype, | ||
is_object_dtype, | ||
is_iterator, | ||
is_list_like, | ||
pandas_dtype, | ||
is_scalar) | ||
from pandas.core.dtypes.missing import isna, array_equivalent | ||
from pandas.errors import PerformanceWarning, UnsortedIndexError | ||
|
@@ -2715,9 +2717,14 @@ def difference(self, other): | |
|
||
@Appender(_index_shared_docs['astype']) | ||
def astype(self, dtype, copy=True): | ||
if not is_object_dtype(np.dtype(dtype)): | ||
raise TypeError('Setting %s dtype to anything other than object ' | ||
'is not supported' % self.__class__) | ||
dtype = pandas_dtype(dtype) | ||
if is_categorical_dtype(dtype): | ||
msg = '> 1 ndim Categorical are not supported at this time' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. test fo this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wrote a test for it in |
||
raise NotImplementedError(msg) | ||
elif not is_object_dtype(dtype): | ||
msg = ('Setting {cls} dtype to anything other than object ' | ||
'is not supported').format(cls=self.__class__) | ||
raise TypeError(msg) | ||
elif copy is True: | ||
return self._shallow_copy() | ||
return self | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,13 +16,15 @@ | |
is_timedelta64_dtype, | ||
is_period_dtype, | ||
is_bool_dtype, | ||
is_categorical_dtype, | ||
pandas_dtype, | ||
_ensure_object) | ||
from pandas.core.dtypes.dtypes import PeriodDtype | ||
from pandas.core.dtypes.generic import ABCSeries | ||
|
||
import pandas.tseries.frequencies as frequencies | ||
from pandas.tseries.frequencies import get_freq_code as _gfc | ||
from pandas.core.indexes.category import CategoricalIndex | ||
from pandas.core.indexes.datetimes import DatetimeIndex, Int64Index, Index | ||
from pandas.core.indexes.timedeltas import TimedeltaIndex | ||
from pandas.core.indexes.datetimelike import DatelikeOps, DatetimeIndexOpsMixin | ||
|
@@ -517,6 +519,9 @@ def astype(self, dtype, copy=True, how='start'): | |
return self.to_timestamp(how=how).tz_localize(dtype.tz) | ||
elif is_period_dtype(dtype): | ||
return self.asfreq(freq=dtype.freq) | ||
elif is_categorical_dtype(dtype): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same side thing, I think that we could make a more generic astype in |
||
return CategoricalIndex(self.values, name=self.name, dtype=dtype, | ||
copy=copy) | ||
raise TypeError('Cannot cast PeriodIndex to dtype %s' % dtype) | ||
|
||
@Substitution(klass='PeriodIndex') | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed this because switching to
pandas_dtype
caused a test to break since it was passing'interval'
as the dtype, which appears to be valid: