Skip to content

Commit b98ab11

Browse files
committed
Inconsistent behaviour when calling apply() on a categorical column with missing data pandas-dev#20714 SOLVED
1 parent 35dd15b commit b98ab11

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

pandas/core/arrays/categorical.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1198,7 +1198,7 @@ def map(self, mapper):
11981198
categories=new_categories,
11991199
ordered=self.ordered)
12001200
except ValueError:
1201-
return np.take(new_categories, self._codes)
1201+
return take_1d(new_categories, self._codes)
12021202

12031203
__eq__ = _cat_compare_op('__eq__')
12041204
__ne__ = _cat_compare_op('__ne__')

pandas/tests/series/test_apply.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,15 @@ def test_apply_dict_depr(self):
164164
with tm.assert_produces_warning(FutureWarning):
165165
tsdf.A.agg({'foo': ['sum', 'mean']})
166166

167+
def test_apply_categorical_with_nan_values(self):
168+
# GH 20714
169+
s1 = pd.Series(['1-1', '1-1', np.NaN], dtype='category')
170+
s1 = s1.apply(lambda x: x.split('-')[0])
171+
172+
s2 = pd.Series(['1', '1', np.NaN], dtype='category')
173+
174+
tm.assert_series_equal(s1, s2, check_dtype=False)
175+
167176

168177
class TestSeriesAggregate(TestData):
169178

0 commit comments

Comments
 (0)