Skip to content

Commit ef5ed65

Browse files
committed
simplity implementation
1 parent afb843b commit ef5ed65

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

pandas/core/arrays/categorical.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -1301,18 +1301,18 @@ def map(
13011301

13021302
new_categories = self.categories.map(mapper)
13031303

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

1306-
if na_action is None and not_dictlike_and_no_nans and np.any(self._codes == -1):
1307-
na_value = mapper(np.nan) if callable(mapper) else mapper[np.nan]
1308-
new_categories = new_categories.insert(len(new_categories), na_value)
1309-
return np.take(new_categories, self._codes)
1310-
elif new_categories.is_unique and not new_categories.hasnans:
1306+
na_val = np.nan
1307+
if na_action is None and has_nans:
1308+
na_val = mapper(np.nan) if callable(mapper) else mapper.get(np.nan, np.nan)
1309+
1310+
if new_categories.is_unique and not new_categories.hasnans and na_val is np.nan:
13111311
new_dtype = CategoricalDtype(new_categories, ordered=self.ordered)
13121312
return self.from_codes(self._codes.copy(), dtype=new_dtype)
13131313

1314-
if np.any(self._codes == -1):
1315-
new_categories = new_categories.insert(len(new_categories), np.nan)
1314+
if has_nans:
1315+
new_categories = new_categories.insert(len(new_categories), na_val)
13161316

13171317
return np.take(new_categories, self._codes)
13181318

0 commit comments

Comments
 (0)