File tree Expand file tree Collapse file tree 2 files changed +9
-9
lines changed Expand file tree Collapse file tree 2 files changed +9
-9
lines changed Original file line number Diff line number Diff line change @@ -41,7 +41,7 @@ Other enhancements
41
41
- Improved error message when creating a DataFrame with empty data (0 rows), no index and an incorrect number of columns. (:issue: `52084 `)
42
42
- :meth: `arrays.SparseArray.map ` now supports ``na_action `` (:issue: `52096 `).
43
43
- :meth: `Categorical.map ` and :meth: `CategoricalIndex.map ` now have a ``na_action `` parameter (:issue: `44279 `)
44
- -
44
+
45
45
46
46
.. ---------------------------------------------------------------------------
47
47
.. _whatsnew_210.notable_bug_fixes :
Original file line number Diff line number Diff line change @@ -1294,18 +1294,18 @@ def map(
1294
1294
1295
1295
new_categories = self .categories .map (mapper )
1296
1296
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 )
1298
1298
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 :
1304
1304
new_dtype = CategoricalDtype (new_categories , ordered = self .ordered )
1305
1305
return self .from_codes (self ._codes .copy (), dtype = new_dtype )
1306
1306
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 )
1309
1309
1310
1310
return np .take (new_categories , self ._codes )
1311
1311
You can’t perform that action at this time.
0 commit comments