Skip to content

Commit

Permalink
REF: use another ctor for fastpath
Browse files Browse the repository at this point in the history
Also use __init__ instead of __new__ for simplicity.
  • Loading branch information
TomAugspurger committed Sep 15, 2017
1 parent 7314570 commit 91752fe
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
4 changes: 2 additions & 2 deletions pandas/core/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -969,8 +969,8 @@ def remove_unused_categories(self, inplace=False):
idx, inv = idx[1:], inv - 1

new_categories = cat.dtype.categories.take(idx)
new_dtype = CategoricalDtype(new_categories, ordered=self.ordered,
fastpath=True)
new_dtype = CategoricalDtype._from_fastpath(new_categories,
ordered=self.ordered)
cat._dtype = new_dtype
cat._codes = coerce_indexer_dtype(inv, new_dtype.categories)

Expand Down
21 changes: 14 additions & 7 deletions pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,25 @@ class CategoricalDtype(ExtensionDtype):
_metadata = ['categories', 'ordered']
_cache = {}

def __new__(cls, categories=None, ordered=False, fastpath=False):
def __init__(self, categories=None, ordered=False):
self._finalize(categories, ordered, fastpath=False)

@classmethod
def _from_fastpath(cls, categories=None, ordered=False):
self = cls.__new__(cls)
self._finalize(categories, ordered, fastpath=True)
return self

def _finalize(self, categories, ordered, fastpath=False):
from pandas.core.indexes.base import Index

if categories is not None:
categories = Index(categories, tupleize_cols=False)
# validation
cls._validate_categories(categories, fastpath=fastpath)
cls._validate_ordered(ordered)
categorical = object.__new__(cls)
categorical._categories = categories
categorical._ordered = ordered
return categorical
self._validate_categories(categories)
self._validate_ordered(ordered)
self._categories = categories
self._ordered = ordered

def __hash__(self):
# _hash_categories returns a uint64, so use the negative
Expand Down

0 comments on commit 91752fe

Please sign in to comment.