Skip to content

CLN: use 'codes' rather than 'values' internally in Categorical #22547

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 1 commit into from
Aug 31, 2018
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
26 changes: 12 additions & 14 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1736,15 +1736,15 @@ def fillna(self, value=None, method=None, limit=None):
raise NotImplementedError("specifying a limit for fillna has not "
"been implemented yet")

values = self._codes
codes = self._codes

# pad / bfill
if method is not None:

values = self.to_dense().reshape(-1, len(self))
values = interpolate_2d(values, method, 0, None,
value).astype(self.categories.dtype)[0]
values = _get_codes_for_values(values, self.categories)
codes = _get_codes_for_values(values, self.categories)

else:

Expand All @@ -1756,27 +1756,27 @@ def fillna(self, value=None, method=None, limit=None):

values_codes = _get_codes_for_values(value, self.categories)
indexer = np.where(values_codes != -1)
values[indexer] = values_codes[values_codes != -1]
codes[indexer] = values_codes[values_codes != -1]

# If value is not a dict or Series it should be a scalar
elif is_hashable(value):
if not isna(value) and value not in self.categories:
raise ValueError("fill value must be in categories")

mask = values == -1
mask = codes == -1
if mask.any():
values = values.copy()
codes = codes.copy()
if isna(value):
values[mask] = -1
codes[mask] = -1
else:
values[mask] = self.categories.get_loc(value)
codes[mask] = self.categories.get_loc(value)

else:
raise TypeError('"value" parameter must be a scalar, dict '
'or Series, but you passed a '
'"{0}"'.format(type(value).__name__))

return self._constructor(values, dtype=self.dtype, fastpath=True)
return self._constructor(codes, dtype=self.dtype, fastpath=True)

def take_nd(self, indexer, allow_fill=None, fill_value=None):
"""
Expand Down Expand Up @@ -2148,14 +2148,12 @@ def mode(self, dropna=True):
"""

import pandas._libs.hashtable as htable
values = self._codes
codes = self._codes
if dropna:
good = self._codes != -1
values = self._codes[good]
values = sorted(htable.mode_int64(ensure_int64(values), dropna))
result = self._constructor(values=values, dtype=self.dtype,
fastpath=True)
return result
codes = self._codes[good]
codes = sorted(htable.mode_int64(ensure_int64(codes), dropna))
return self._constructor(values=codes, dtype=self.dtype, fastpath=True)

def unique(self):
"""
Expand Down
4 changes: 1 addition & 3 deletions pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,9 +460,7 @@ def where(self, cond, other=None):
other = self._na_value
values = np.where(cond, self.values, other)

cat = Categorical(values,
categories=self.categories,
ordered=self.ordered)
cat = Categorical(values, dtype=self.dtype)
Copy link
Member

Choose a reason for hiding this comment

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

Hey- why can we get rid of the categories and ordered params out of interest?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The dtype is a CategoricalDtype, which contains attributes categories and ordered. So we're not really removing them, only passinf the dtype around instead, which is clearer IMO.

return self._shallow_copy(cat, **self._get_attributes_dict())

def reindex(self, target, method=None, level=None, limit=None,
Expand Down