Skip to content

CLN: make CategoricalIndex._create_categorical a classmethod #21618

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,8 @@ def to_frame(self, index=True):
"""

from pandas import DataFrame
result = DataFrame(self._shallow_copy(), columns=[self.name or 0])
Copy link
Contributor Author

@topper-123 topper-123 Jun 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_shallow_copy does not guarantee that _data is copied, so a test fails. to_frame should get a copy, else smething like this could happen:

>>> ci = pd.CategoricalIndex(['a', 'b', 'c'])
>>> df = ci.to_frame()
>>> df.iloc[0] = 'c'
>>> df
   0
c  c  # index label changed!
b  b
c  c

By using self.values.copy() rather than _shallow_copy, this issue is avoided.

name = self.name or 0
result = DataFrame({name: self.values.copy()})

if index:
result.index = self
Expand Down
18 changes: 9 additions & 9 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ def __new__(cls, data=None, categories=None, ordered=None, dtype=None,
name = data.name

if isinstance(data, ABCCategorical):
data = cls._create_categorical(cls, data, categories, ordered,
data = cls._create_categorical(data, categories, ordered,
dtype)
elif isinstance(data, CategoricalIndex):
data = data._data
data = cls._create_categorical(cls, data, categories, ordered,
data = cls._create_categorical(data, categories, ordered,
dtype)
else:

Expand All @@ -99,7 +99,7 @@ def __new__(cls, data=None, categories=None, ordered=None, dtype=None,
if data is not None or categories is None:
cls._scalar_data_error(data)
data = []
data = cls._create_categorical(cls, data, categories, ordered,
data = cls._create_categorical(data, categories, ordered,
dtype)

if copy:
Expand Down Expand Up @@ -136,8 +136,8 @@ def _create_from_codes(self, codes, categories=None, ordered=None,
ordered=self.ordered)
return CategoricalIndex(cat, name=name)

@staticmethod
def _create_categorical(self, data, categories=None, ordered=None,
@classmethod
def _create_categorical(cls, data, categories=None, ordered=None,
dtype=None):
"""
*this is an internal non-public method*
Expand All @@ -155,7 +155,7 @@ def _create_categorical(self, data, categories=None, ordered=None,
-------
Categorical
"""
if (isinstance(data, (ABCSeries, type(self))) and
if (isinstance(data, (cls, ABCSeries)) and
is_categorical_dtype(data)):
data = data.values

Expand All @@ -179,7 +179,7 @@ def _simple_new(cls, values, name=None, categories=None, ordered=None,
dtype=None, **kwargs):
result = object.__new__(cls)

values = cls._create_categorical(cls, values, categories, ordered,
values = cls._create_categorical(values, categories, ordered,
dtype=dtype)
result._data = values
result.name = name
Expand Down Expand Up @@ -236,7 +236,7 @@ def _is_dtype_compat(self, other):
if not is_list_like(values):
values = [values]
other = CategoricalIndex(self._create_categorical(
self, other, categories=self.categories, ordered=self.ordered))
other, categories=self.categories, ordered=self.ordered))
if not other.isin(values).all():
raise TypeError("cannot append a non-category item to a "
"CategoricalIndex")
Expand Down Expand Up @@ -798,7 +798,7 @@ def _evaluate_compare(self, other):
other = other._values
elif isinstance(other, Index):
other = self._create_categorical(
self, other._values, categories=self.categories,
other._values, categories=self.categories,
ordered=self.ordered)

if isinstance(other, (ABCCategorical, np.ndarray,
Expand Down