Skip to content

PERF: removed coercion to int64 for arrays of ints in Categorical.from_codes #21000

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 3 commits into from
May 15, 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
4 changes: 4 additions & 0 deletions asv_bench/benchmarks/categoricals.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def setup(self):

self.values_some_nan = list(np.tile(self.categories + [np.nan], N))
self.values_all_nan = [np.nan] * len(self.values)
self.values_all_int8 = np.ones(N, 'int8')

def time_regular(self):
pd.Categorical(self.values, self.categories)
Expand All @@ -70,6 +71,9 @@ def time_with_nan(self):
def time_all_nan(self):
pd.Categorical(self.values_all_nan)

def time_from_codes_all_int8(self):
pd.Categorical.from_codes(self.values_all_int8, self.categories)


class ValueCounts(object):

Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,7 @@ Performance Improvements
- Improved performance of :func:`Series.isin` in the case of categorical dtypes (:issue:`20003`)
- Improved performance of ``getattr(Series, attr)`` when the Series has certain index types. This manifiested in slow printing of large Series with a ``DatetimeIndex`` (:issue:`19764`)
- Fixed a performance regression for :func:`GroupBy.nth` and :func:`GroupBy.last` with some object columns (:issue:`19283`)
- Improved performance of :func:`pandas.core.arrays.Categorical.from_codes` (:issue:`18501`)

.. _whatsnew_0230.docs:

Expand Down
2 changes: 1 addition & 1 deletion pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ def from_codes(cls, codes, categories, ordered=False):
unordered.
"""
try:
codes = np.asarray(codes, np.int64)
codes = coerce_indexer_dtype(np.asarray(codes), categories)
except (ValueError, TypeError):
raise ValueError(
"codes need to be convertible to an arrays of integers")
Expand Down