Skip to content

Commit dc9d119

Browse files
committed
simplity implementation
1 parent 1220145 commit dc9d119

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

doc/source/whatsnew/v2.1.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Other enhancements
4141
- Improved error message when creating a DataFrame with empty data (0 rows), no index and an incorrect number of columns. (:issue:`52084`)
4242
- :meth:`arrays.SparseArray.map` now supports ``na_action`` (:issue:`52096`).
4343
- :meth:`Categorical.map` and :meth:`CategoricalIndex.map` now have a ``na_action`` parameter (:issue:`44279`)
44-
-
44+
4545

4646
.. ---------------------------------------------------------------------------
4747
.. _whatsnew_210.notable_bug_fixes:

pandas/core/arrays/categorical.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,18 +1294,18 @@ def map(
12941294

12951295
new_categories = self.categories.map(mapper)
12961296

1297-
not_dictlike_and_no_nans = not (is_dict_like(mapper) and np.nan not in mapper)
1297+
has_nans = np.any(self._codes == -1)
12981298

1299-
if na_action is None and not_dictlike_and_no_nans and np.any(self._codes == -1):
1300-
na_value = mapper(np.nan) if callable(mapper) else mapper[np.nan]
1301-
new_categories = new_categories.insert(len(new_categories), na_value)
1302-
return np.take(new_categories, self._codes)
1303-
elif new_categories.is_unique and not new_categories.hasnans:
1299+
na_val = np.nan
1300+
if na_action is None and has_nans:
1301+
na_val = mapper(np.nan) if callable(mapper) else mapper.get(np.nan, np.nan)
1302+
1303+
if new_categories.is_unique and not new_categories.hasnans and na_val is np.nan:
13041304
new_dtype = CategoricalDtype(new_categories, ordered=self.ordered)
13051305
return self.from_codes(self._codes.copy(), dtype=new_dtype)
13061306

1307-
if np.any(self._codes == -1):
1308-
new_categories = new_categories.insert(len(new_categories), np.nan)
1307+
if has_nans:
1308+
new_categories = new_categories.insert(len(new_categories), na_val)
13091309

13101310
return np.take(new_categories, self._codes)
13111311

0 commit comments

Comments
 (0)