Skip to content

Commit 5a07694

Browse files
committed
Address changes requested by @jreback
1 parent 90db68b commit 5a07694

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

asv_bench/benchmarks/series_methods.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,11 @@ def setup(self, mapper, dtype):
157157
self.map_data = map_data
158158
elif mapper == 'dict':
159159
self.map_data = map_data.to_dict()
160-
else:
160+
elif mapper == 'lambda':
161161
map_dict = map_data.to_dict()
162162
self.map_data = lambda x: map_dict[x]
163+
else:
164+
raise NotImplementedError
163165

164166
self.s = Series(np.random.randint(0, map_size, 10000), dtype=dtype)
165167

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ Performance Improvements
247247
- Improved performance of :meth:`DataFrame.to_csv` when writing datetime dtypes (:issue:`25708`)
248248
- Improved performance of :meth:`read_csv` by much faster parsing of ``MM/YYYY`` and ``DD/MM/YYYY`` datetime formats (:issue:`25922`)
249249
- Improved performance of :meth:`pandas.core.base.IndexOpsMixin._map_values` for dictionary mappers on categorical series by using the default series map method (:issue:`23785`)
250+
- Improved performance of :meth:`Series.map` for dictionary mappers on categorical series by mapping the categories instead of mapping all values (:issue:`23785`)
250251

251252
.. _whatsnew_0250.bug_fixes:
252253

pandas/core/base.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -1183,16 +1183,15 @@ def _map_values(self, mapper, na_action=None):
11831183
if isinstance(mapper, ABCSeries):
11841184
# Since values were input this means we came from either
11851185
# a dict or a series and mapper should be an index
1186+
if is_categorical_dtype(self._values):
1187+
# use the built in categorical series mapper which saves
1188+
# time by mapping the categories instead of all values
1189+
return self._values.map(mapper)
11861190
if is_extension_type(self.dtype):
11871191
values = self._values
11881192
else:
11891193
values = self.values
11901194

1191-
if is_categorical_dtype(values):
1192-
# use the built in categorical series mapper which saves
1193-
# time by mapping the categories instead of all values
1194-
return values.map(mapper)
1195-
11961195
indexer = mapper.index.get_indexer(values)
11971196
new_values = algorithms.take_1d(mapper._values, indexer)
11981197

0 commit comments

Comments
 (0)