Skip to content

Commit 363426f

Browse files
nlee737TomAugspurger
authored andcommitted
PERF: removed coercion to int64 for arrays of ints in Categorical.from_codes (#21000)
1 parent d007c2d commit 363426f

File tree

3 files changed

+6
-1
lines changed

3 files changed

+6
-1
lines changed

asv_bench/benchmarks/categoricals.py

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def setup(self):
5151

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

5556
def time_regular(self):
5657
pd.Categorical(self.values, self.categories)
@@ -70,6 +71,9 @@ def time_with_nan(self):
7071
def time_all_nan(self):
7172
pd.Categorical(self.values_all_nan)
7273

74+
def time_from_codes_all_int8(self):
75+
pd.Categorical.from_codes(self.values_all_int8, self.categories)
76+
7377

7478
class ValueCounts(object):
7579

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,7 @@ Performance Improvements
10801080
- Improved performance of :func:`Series.isin` in the case of categorical dtypes (:issue:`20003`)
10811081
- 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`)
10821082
- Fixed a performance regression for :func:`GroupBy.nth` and :func:`GroupBy.last` with some object columns (:issue:`19283`)
1083+
- Improved performance of :func:`pandas.core.arrays.Categorical.from_codes` (:issue:`18501`)
10831084

10841085
.. _whatsnew_0230.docs:
10851086

pandas/core/arrays/categorical.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ def from_codes(cls, codes, categories, ordered=False):
578578
unordered.
579579
"""
580580
try:
581-
codes = np.asarray(codes, np.int64)
581+
codes = coerce_indexer_dtype(np.asarray(codes), categories)
582582
except (ValueError, TypeError):
583583
raise ValueError(
584584
"codes need to be convertible to an arrays of integers")

0 commit comments

Comments
 (0)